Crystal: "require" resolves nonsensical relative paths

Created on 10 Feb 2019  路  3Comments  路  Source: crystal-lang/crystal

In a filesystem structure such as

src
|-- foo.cr
spec
|-- spec_helper.cr
|-- foo_spec.cr

where spec_helper.cr contains:

require "spec"
require "../../src/foo" # << This path is wrong and doesn't exist.
# ...

The above code does _not_ result in a compile time error but instead requires the file src/foo.cr.
This is highly confusing in my opinion and should be a compile time error.

I can't quite catch this behaviour from https://crystal-lang.org/reference/syntax_and_semantics/requiring_files.html. I do believe the code is running into this https://github.com/crystal-lang/crystal/blob/master/src/compiler/crystal/crystal_path.cr#L71 line and is effectively working because ../src/../src/foo.cr is found, but still I think this shouldn't resolve, since require "../../src/foo from inside spec_helper is simply plain wrong.

I am running on macOS:

Crystal 0.27.2 (2019-02-05)

LLVM: 6.0.1
Default target: x86_64-apple-macosx

but obviously the code is still the same in HEAD.

bug compiler

Most helpful comment

I updated the issue to show the filestructure more clearly and removed the other unnecessary files. I want to require foo, but the path I entered is wrong (one .. too many) so I expect this to error, which it doesn't.

@ysbaddaden I think you understood correctly, that's what I'm seeing.

All 3 comments

Sorry, could you show the directory tree you have, where is spec_helper.cr, which file you want to require and which file is effectively required?

If I understand correctly, the issue that require considers that ../../src/spec_helper could belong to a shard named .. and will search lib/../src/../src/spec_helper.cr but .. is a relative path, and require should only search for a relative ../../src/spec_helper.cr file.

I updated the issue to show the filestructure more clearly and removed the other unnecessary files. I want to require foo, but the path I entered is wrong (one .. too many) so I expect this to error, which it doesn't.

@ysbaddaden I think you understood correctly, that's what I'm seeing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oprypin picture oprypin  路  3Comments

pbrusco picture pbrusco  路  3Comments

lbguilherme picture lbguilherme  路  3Comments

lgphp picture lgphp  路  3Comments

ArthurZ picture ArthurZ  路  3Comments