Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"require" resolves nonsensical relative paths #7408

Closed
hanneskaeufler opened this issue Feb 10, 2019 · 3 comments · Fixed by #7758
Closed

"require" resolves nonsensical relative paths #7408

hanneskaeufler opened this issue Feb 10, 2019 · 3 comments · Fixed by #7758
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler

Comments

@hanneskaeufler
Copy link
Contributor

hanneskaeufler commented Feb 10, 2019

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.

@asterite
Copy link
Member

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?

@ysbaddaden
Copy link
Contributor

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.

@asterite asterite added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler labels Feb 14, 2019
@hanneskaeufler
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants