www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Executing a D script without an [extension in the filename] leads to

reply BoQsc <vaidas.boqsc gmail.com> writes:
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 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?
Mar 01 2019
parent reply Cym13 <cpicard openmailbox.org> writes:
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 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: And indeed rdmd won't call your script if it doesn't have the proper extension. Try using this instead:
Mar 01 2019
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
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 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: And indeed rdmd won't call your script if it doesn't have the proper extension. Try using this instead:
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: "And indeed rdmd won't call your script if it doesn't have the proper extension." -run) Is that an error/mistake in Dlang Tour guide?
Mar 01 2019
next sibling parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
On Friday, 1 March 2019 at 11:38:51 UTC, BoQsc wrote:
 "And indeed rdmd won't call your script if it doesn't have the 
 proper extension."



 Instead of the one you mentioned, that is fool proof. 


 Is that an error/mistake in Dlang Tour guide?
You may want to change that too: DMD doesn't automatically compile imported files (at least not until -i came along), rdmd existed to solve that problem... I don't know what value it brings with the -i switch existing.
Mar 01 2019
next sibling parent reply Seb <seb wilzba.ch> writes:
On Friday, 1 March 2019 at 14:50:45 UTC, Jesse Phillips wrote:
 I don't know what value it brings with the -i switch existing.
Almost none, except that it's twice as slow as DMD because it needs to run DMD twice to learn about all the dependencies. It's only useful for a few small things now: - '-e': evaluate d code with all modules automatically imported (though now that there's std.experimental.all this value is gone too) - makefile deps generation - shebang line (as arguments can't be part of the shebang line) (The list is not complete)
Mar 01 2019
parent Seb <seb wilzba.ch> writes:
On Friday, 1 March 2019 at 16:45:38 UTC, Seb wrote:
 On Friday, 1 March 2019 at 14:50:45 UTC, Jesse Phillips wrote:
 I don't know what value it brings with the -i switch existing.
Almost none, except that it's twice as slow as DMD because it needs to run DMD twice to learn about all the dependencies. It's only useful for a few small things now: - '-e': evaluate d code with all modules automatically imported (though now that there's std.experimental.all this value is gone too) - makefile deps generation - shebang line (as arguments can't be part of the shebang line) (The list is not complete)
I forgot one big reason why rdmd is still nice: caching. It does save the generated dependency file list and checks all modification time stamps, s.t. if nothing has changed, it'll run a cached build
Mar 01 2019
prev sibling parent Cym13 <cpicard openmailbox.org> writes:
On Friday, 1 March 2019 at 14:50:45 UTC, Jesse Phillips wrote:
 On Friday, 1 March 2019 at 11:38:51 UTC, BoQsc wrote:
 "And indeed rdmd won't call your script if it doesn't have the 
 proper extension."



 Instead of the one you mentioned, that is fool proof. 


 Is that an error/mistake in Dlang Tour guide?
You may want to change that too: DMD doesn't automatically compile imported files (at least not until -i came along), rdmd existed to solve that problem... I don't know what value it brings with the -i switch existing.
All systems I know only accept one argument in shebang so sadly it's not that simple :)
Mar 01 2019
prev sibling next sibling parent Seb <seb wilzba.ch> writes:
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:
 [...]
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: And indeed rdmd won't call your script if it doesn't have the proper extension. Try using this instead:
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: "And indeed rdmd won't call your script if it doesn't have the proper extension." Instead of the one you mentioned, that is fool proof. Is that an error/mistake in Dlang Tour guide?
Well, because it isn't fool proof either ;-) It won't work once you start using more files as you would then need the -i flag , but unfortunately most systems don't support more than one shebang argument. I think it's simply a missing feature of rdmd to accept files without an extension as D programs (though of course the tour could be improved to be more explanatory here too).
Mar 01 2019
prev sibling parent Cym13 <cpicard openmailbox.org> writes:
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 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: And indeed rdmd won't call your script if it doesn't have the proper extension. Try using this instead:
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: "And indeed rdmd won't call your script if it doesn't have the proper extension." Instead of the one you mentioned, that is fool proof. 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": 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.
Mar 01 2019