nim-lang / nim-mode

An emacs major mode for the Nim programming language
137 stars 46 forks source link

nim-compile fails when file path includes spaces #240

Closed bhrgunatha closed 10 months ago

bhrgunatha commented 2 years ago

Conforming Bugs

Please tell us below:

> nim --version
Nim Compiler Version 1.6.2 [Linux: amd64]
Compiled at 2021-12-17
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 9084d9bc02bcd983b81a4c76a05f27b9ce2707dd
active boot switches: -d:release

> nim-suggest --version
Nim Compiler Version 1.6.2 [Linux: amd64]
Compiled at 2021-12-17
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 9084d9bc02bcd983b81a4c76a05f27b9ce2707dd
active boot switches: -d:release -d:danger --gc:markAndSweep

This is my configuration of nim-mode

(use-package nim-mode
  :hook (nim-mode . lsp-deferred)
  :config
  (setq nim-compile-command "/home/bhrgunatha/.nimble/bin/nim"
        nimsuggest-path "/home/bhrgunatha/.nimble/bin/"))

I'm new to Nim and thought I'd look at how people solved the recent advent of code puzzles to get a feel for the language. and the first one I tried was https://github.com/lscrd/AdventOfCode2021

When I try to compile a program - C-c C-c - that has spaces in the path I get the following error.

-*- mode: nim-compile; default-directory: "/mnt/data/src/example repos/nim/advent-of-code/2021.lscrd/Day 01/" -*-
nim-compile started at Sun Jan 23 11:23:47

/home/bhrgunatha/.nimble/bin/nim c -r --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on /mnt/data/src/example\\\ repos/nim/advent-of-code/2021.lscrd/Day\\\ 01/p01.nim
oserr.nim(95)            raiseOSError
Error: unhandled exception: No such file or directory
Additional info: /mnt/data/src/example\ repos/nim/advent-of-code/2021.lscrd/Day\ 01 [OSError]

nim-compile exited abnormally with code 1 at Sun Jan 23 11:23:47

If I move the file to a path without spaces it compiles successfully.

Directly on the command line:

/home/bhrgunatha/.nimble/bin/nim c -r --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on /mnt/data/src/example\\\ repos/nim/advent-of-code/2021.lscrd/Day\\\ 01/p01.nim

fails

/home/bhrgunatha/.nimble/bin/nim c -r --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on /mnt/data/src/example\\ repos/nim/advent-of-code/2021.lscrd/Day\\ 01/p01.nim

fails and

/home/bhrgunatha/.nimble/bin/nim c -r --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on /mnt/data/src/example\ repos/nim/advent-of-code/2021.lscrd/Day\ 01/p01.nim

succeeds.

That escaping of the spaces "\\\ " looks strange to me and it seems like nim-compile is calling shell-quote-argument

(shell-quote-argument "/mnt/data/src/example repos/nim/advent-of-code/2021.lscrd/Day 01/p01.nim")
"/mnt/data/src/example\\ repos/nim/advent-of-code/2021.lscrd/Day\\ 01/p01.nim"

but that's as far as I got.

If I edit the minibuffer before the command is executed to use a single \ it works fine

/home/bhrgunatha/.nimble/bin/nim c -r --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on /mnt/data/src/example\ repos/nim/advent-of-code/2021.lscrd/Day\ 01/p01.nim

but surrounding the path with quotes doesn't

/home/bhrgunatha/.nimble/bin/nim c -r --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on "/mnt/data/src/example repos/nim/advent-of-code/2021.lscrd/Day 01/p01.nim"
bhrgunatha commented 2 years ago

After some debugging I think the culprit is perhaps nim--fmt at least for the path my use takes.

Initially nim-compile--get-compile-command doesn't find a saved command so it assigns file using

(when buffer-file-name
           (shell-quote-argument buffer-file-name))

It's using shell-quote-argument already. Later nim--fmt gets called with:

nim--fmt (c -r --verbosity:0 --hint[Processing]:off --excessiveStackTrace:on) /mnt/data/src/example\ repos/nim/advent-of-code/2021.lscrd/Day\ 01/p01.nim

The file path is correctly quoted already by shell-quote-argument.

It then maps shell-quote-argument again over the arguments and the file - which is why I think the spaces are getting handled as "\\\ " in the file path.

I don't know enough about the other things nim-compile has to deal with so can't suggest a good fix but I think I've found the issue at least.
.