2

Kotlin compiler is located in C:\kotlinc\bin, when trying to run any script that is located in a path with space - it fails with message

InvalidPathException: Illegal char <:> at index 2: /H:/My%20Drive/...

Doesn't matter if I use relative path, just file name, full path, with or without quotes - the only fix is to move kotlin AND the script to location without space char in it. Tested in windows powershell and git bash. Examples of calls:

kotlin test.kts
kotlin "test.kts"
kotlin ./test.kts
kotlin "./test.kts"
kotlin "H:\my drive\test.kts"

and none of them work. The same error message is shown when:

  1. kotlin.bat path contains space
  2. "kotlin" command alone is executed in the path that has a space
  3. "kotlin test.kts" when test.kts has space in absolute path

How is it possible that such basic bug exists or how to solve it?

6
  • 1
    Remember that nobody can see your screen. You need to post the exact command you are trying to run. In general, on Windows, you would place double-quote characters (") around a command path that contains spaces. Commented Nov 18 at 18:39
  • I made a mistake calling quotes parenthesis, but the description included all the cases I just added to the post Commented Nov 18 at 19:59
  • I literally included error message -_- even in the original version of the post: InvalidPathException: Illegal char <:> at index 2: /H:/My%20Drive/... Commented Nov 18 at 20:56
  • This appears to be a known Kotlin bug: #2503: Kotlin asset compiler errors with spaces in paths: This appears to be an issue in Node - kotlinc-js-api calls child_process.spawn, which doesn't seem to work when both arguments and the path have spaces (github.com/nodejs/node/issues/7367). I recommend to push for a solution in those GitHub issues. Commented Nov 19 at 8:59
  • I don't have Kotlin installed, but something like kotlin """$pwd\test.kts""" might workaround it in PowerShell. Otherwise, this might also workaround the issue in PowerShell: $fso = New-Object -ComObject Scripting.FileSystemObject; kotlin $fso.getfile("$pwd\test.kts").ShortPath Commented Nov 19 at 9:02

2 Answers 2

2
  • kotlin-compiler-2.2.21.zip

  • Environment Variable: KOTLIN_HOME

    • KOTLIN_HOME -> D:\TOOLS\kotlinc
  • Environment Variable: PATH

    • PATH -> D:\TOOLS\kotlinc\bin
  • D:\My Drive\Test Script\test.kts

println("I am runnable!")

Test

Open CMD.exe

Test OK

  • kotlin "D:\\My Drive\\Test Script\\test.kts"
  • kotlin "D:/My Drive/Test Script/test.kts"
  • kotlin "D:\My Drive\Test Script\test.kts"
Microsoft Windows [Version 10.0.17763.1935]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\IEUser>kotlin "D:\\My Drive\\Test Script\\test.kts"
I am runnable!

C:\Users\IEUser>kotlin "D:/My Drive/Test Script/test.kts"
I am runnable!

C:\Users\IEUser>kotlin "D:\My Drive\Test Script\test.kts"
I am runnable!

Test Fail

Open CMD.exe

Run kotlin under directory path contain space char.

D:
cd "D:\My Drive\Test Script"

kotlin "test.kts"
D:\My Drive\Test Script>kotlin "test.kts"
exception: java.nio.file.InvalidPathException: Illegal char <:> at index 2: /D:/My%20Drive/Test%20Script/./
        at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
        at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
        at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92)
        at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:232)
        at java.base/java.nio.file.Path.of(Path.java:147)
        at java.base/java.nio.file.Paths.get(Paths.java:69)

enter image description here

kotlin.bat

@echo off
setlocal EnableDelayedExpansion

REM ==========================================
REM Part 1: Display Directory Information
REM ==========================================

echo ---------------------------------------------------
echo [INFO] Script location: "%~dp0"
echo [INFO] Working directory: "%cd%"
echo ---------------------------------------------------

REM ==========================================
REM Part 2: Process Arguments One by One
REM ==========================================

set "NEW_ARGS="

:ProcessArgs
if "%~1"=="" goto :EndArgs

set "CURRENT_ARG=%~1"
set "CURRENT_EXT=%~x1"
set "IS_KOTLIN=0"

if /i "!CURRENT_EXT!"==".kt" set "IS_KOTLIN=1"
if /i "!CURRENT_EXT!"==".kts" set "IS_KOTLIN=1"

if "!IS_KOTLIN!"=="1" (
    REM Convert to absolute path
    set "PROCESSED_ARG="%~f1""
    echo [PROCESSING] Kotlin file found: "%~1" -^> Converted to: !PROCESSED_ARG!
) else (
    set "PROCESSED_ARG=%1"
)

if defined NEW_ARGS (
    set "NEW_ARGS=!NEW_ARGS! !PROCESSED_ARG!"
) else (
    set "NEW_ARGS=!PROCESSED_ARG!"
)

shift /1
goto :ProcessArgs

:EndArgs

REM ==========================================
REM Part 3: The Handover
REM ==========================================

echo.
echo [INFO] Passing arguments to kotlinc.bat:
echo !NEW_ARGS!
echo ---------------------------------------------------

REM Pass variable out of setlocal
for /f "delims=" %%a in ("!NEW_ARGS!") do (
    endlocal
    set "FINAL_ARGS=%%a"
)

REM ==========================================
REM Part 4: Call the Official Script (SAFE MODE)
REM ==========================================

rem Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.

setlocal
set _KOTLIN_RUNNER=1

REM --- CRITICAL FIX ---
REM The Kotlin Runner crashes if the Current Working Directory contains spaces (InvalidPathException).
REM We temporarily switch directory (pushd) to the script's own folder (%~dp0), 
REM which is usually a safe path like "D:\TOOLS\kotlinc\bin\", to avoid the crash.

pushd "%~dp0"

REM echo [INFO] Temporarily switched to safe directory: "%cd%"

REM Call kotlinc.bat (it is in the current directory now)
call kotlinc.bat %FINAL_ARGS%

popd

This is a modified kotlin.bat file that allows you to execute kotlin test.kts in directory paths containing whitespace characters.

However, it still does not support deploying kotlin in directories containing whitespace characters in the path.

Sign up to request clarification or add additional context in comments.

2 Comments

yep, only quoting whole script path does the thing. 2025, problems with path spaces in a language that was created 9 years ago...
I've provided a modified kotlin.bat file at the end of the answer above. You can test it if you're interested.
1

Even though running kotlin test.kts only works if:

  1. kotlin.bat path doesn't have a space in it

  2. context folder (what pwd shows) doesn't have a space

  3. relative/full path to *.kts file is quoted to escape spaces

I can call:

PS H:\My Drive\project> kotlinc -script test.kts

and it works. What is the point of just kotlin (kotlin.bat)? Idk

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.