Executing a D script without an [extension in the filename] leads to an error
Cym13
cpicard at openmailbox.org
Fri Mar 1 18:25:04 UTC 2019
On Friday, 1 March 2019 at 11:38:51 UTC, BoQsc wrote:
> On Friday, 1 March 2019 at 09:27:33 UTC, Cym13 wrote:
>> On Friday, 1 March 2019 at 09:00:43 UTC, BoQsc wrote:
>>> I've installed D compiler, and when i try to run a D script
>>> with filename without an extension/file type named: program
>>>
>>> via: ./program
>>>
>>> I'm getting and error:
>>>
>>> vaidas at SATELLITE-L855:~/Desktop$ ./program
>>> Error: module `program` is in file './program.d' which cannot
>>> be read
>>> import path[0] = .
>>> import path[1] = /snap/dmd/49/bin/../import/druntime
>>> import path[2] = /snap/dmd/49/bin/../import/phobos
>>> Failed: ["/snap/dmd/49/bin/dmd", "-v", "-o-", "./program.d",
>>> "-I."]
>>>
>>>
>>> Now, when I rename my scirpt file : program
>>> To: program.d
>>>
>>> and execute it by: ./program.d
>>>
>>> I no longer have an error.
>>>
>>> Is this an intended behaviour?
>>
>> In such questions it's important to show your shebang since
>> that's what runs your script.
>>
>> Given your symptoms I guess you're using the following:
>>
>> #!/bin/env rdmd
>>
>> And indeed rdmd won't call your script if it doesn't have the
>> proper extension.
>>
>> Try using this instead:
>>
>> #!/bin/dmd -run
>
> The shebang I used: #!/usr/bin/env rdmd
>
> I was visiting Dlang Tour and that's where I've got an example
> of shebang usage, directly from there:
> https://tour.dlang.org/tour/en/welcome/run-d-program-locally#/on-the-fly-compilation-with-rdmd
>
> The shebang you suggested actually works perfectly:
> #!/bin/dmd -run
>
>
> "And indeed rdmd won't call your script if it doesn't have the
> proper extension."
>
> Then why does Dlang Tour includes shebang: #!/usr/bin/env rdmd
>
> Instead of the one you mentioned, that is fool proof.
> (#!/bin/dmd -run)
>
> Is that an error/mistake in Dlang Tour guide?
Frankly using rdmd is closer to being fool-proof, you just
haven't hit the corner cases yet :)
I'd either get used to having scripts with a .d extension or make
an alias or a quick wrapper in shell and call it "program":
#!/bin/sh
exec rdmd /path/to/program.d "$@"
It's not exactly a mistake, it's just not that important to most
people I guess. And as your program grows you're likely to take
the habit to compile it and work with the binary directly anyway.
More information about the Digitalmars-d-learn
mailing list