The Crystal Programming Language Forum

Problems with "require" and Shards on Windows

After some obstacles I was able to build my own version of the Crystal compiler. (Also: yes, I will add the promised patch for Win7 support, but I was out of time the last weeks.)

The first simple programs are working properly, but then I needed to install libraries with Shards. So I went to compile from source, but it seems Shards needs Shards … for crystal-molinillo. I then put the molinillo source into lib/molinillo, but the compiler still gives me this error message:

C:\Repos\shards>crystal build src\
Showing last frame. Use --error-trace for full trace.

In src\

 1 | require "molinillo"
Error: can't find file 'molinillo'

If you're trying to require a shard:
- Did you remember to run `shards install`?
- Did you make sure you're running the compiler in the same directory as your sh

So, in general, how does require work: where does it try to find the required source code, and why does it work on Linux but not Windows? On Linux the same folder structure in lib is created after shards install molinillo. Shouldn’t lib be browsed by the compiler automatically?

This might be a case of needing to use the Makefile versus doing that directly.

Basically, it falls back on curl if shards isn’t available to install the shard. Should be able to get around this by just replicating what the curl command is doing (assuming the Makefile doesn’t work on windows). Once molinillo is in lib/molinillo you should be good.

I did exactly this. It’s just that require "molinillo" does not find the source at the expected location in lib/molinillo folder, and I don’t know why. Is the lib folder (to be searched) built-in or do I need to set an environment variable?

Ohh yea, good call. There’s a CRYSTAL_PATH env that should also include lib. E.g. lib:/path/to/src. Ofc be sure to update the path to be use Windows directory separators and to the src dir of the stdlib wherever you have it.

Ok, I added lib to the env var, then it compiled properly. Thanks for the hint. I did not know there was a CRYSTAL_PATH env because on Linux there isn’t.

Shards now works up to the point where it needs to create symbolic links, which does not work on Windows (yet?). So I still need to do it manually … but thanks anyway, and Crystal on Windows is on its way.

There is on Linux too, it’s lib:/usr/lib/crystal for me.

See: Using the compiler - Crystal.

Ah I see. crystal env returns lib:/usr/bin/../share/crystal/src for me, but no environment variable is set. So the documented part about “Otherwise the compiler will populate them with default values.” seems to be used.

On Windows the default value seems to be not quite right. I had set CRYSTAL_PATH to only the standard source, which was missing lib. But when I have no variable set, the default value is lib, which is in turn missing the standard source …

Using Crystal on Windows needs a lot of knowledge under the hood. Thanks for pointing me to the env variables.

Not sure if this helps, but I’ve been able to create and compile crystal programs using Ubuntu running in WSL2 on Windows 10. Though it won’t run on the Windows OS directly, I’m still able to develop software with no side effects. Using the code server also allows me to use VSCode as an IDE. You just type code . and it will launch a vscode editor. :grinning:

This is actually not specific to Windows, it’s just how the compiler works. On Linux and MacOS you just don’t interact with the compiler directly because the distribution packages include a wrapper script that does the configuration for you.

I think we still have to figure out how to handle that for windows.

Related discussion: Add `make install` command when building from source · Issue #10702 · crystal-lang/crystal · GitHub

Symbolic links should actually work on Windows. There are some caveats, though. See Windows: pass a flag to allow creating symlinks without admin rights by oprypin · Pull Request #9767 · crystal-lang/crystal · GitHub

I knew there was some magic behind it! Thanks for the hint, I would never have guessed it.

Yes, for some reasons I am still using Windows 7 and Linux (on different computers), so there is no WSL for me to work with. The way I see it, it’s a nice challenge to get Crystal working properly on Win7. Simple tasks already work, more complicated setups still have errors.

I am also using VS Code which is a nice lightweight IDE.

Win10 is a free upgrade from Win7 I believe
I personally found a bunch of old win7 computers in e-waste took photos of their keys and used those keys for VMs on my mac and my gaming computer.

so if you want to upgrade you should be able to