question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@import is ambiguous about relative vs. absolute paths

See original GitHub issue

/cc @hcatlin @akhleung

When Sass (or libsass) encounters @import 'foo', it searches first in the directory of the source file, and then in all the load paths.

Other languages tend to keep relative and absolute imports separate. For instance Ruby has require and require_relative, and Node distinguishes require('foo') (absolute) and require('./foo') (relative).

Quick background why I care: I’m working on a Sass plugin for Broccoli, and one of the big motivators for making Broccoli was making it easier to share frontend JS + CSS code, because we can now use Bower with Broccoli on top. I know people have been distributing Sass files before (e.g. on RubyGems) but I want to see much more of that, and with less friction. I want libraries that depend on other libraries. So now I really care about getting the semantics of @import right.

Having @import be both absolute and relative seems like it might cause problems when there are ambiguities.

I wonder if we should, at some point, change the @import semantics to be unambiguous. For instance, on the next major version of Sass, we could print deprecation warnings on relative @imports that don’t begin with a dot, so that @import 'foo' is absolute, and @import './foo' is relative.

What do you think?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
bughitcommented, Apr 13, 2016

You say it’s ambiguous, but the semantics are well-defined: a relative import is preferred if one exists. This is how Python’s module system works, for example.

So that means that file naming in a sub-folder is constrained by whatever happens to be in the load path, if you want to refer to both the shadow and the shadowed? That does not seem reasonable.

That along with the ambiguity (you can’t tell from the @import statement if it’s relative or not) makes this far from ideal.

Doing relative lookup only for ./ or …/ eliminates the ambiguity and removes naming constraints.

2reactions
jolisscommented, Feb 20, 2014

So I guess what I’m saying is, if I were designing the @import directive from scratch today, I’d probably make it always absolute, unless it begins with ./ or ../.

Now that @import exists already, it’s less clear to me whether it’s worth changing. If the upcoming reworking in 4.0 breaks many imports anyway, it might be worth cleaning this up at the same time, provided that you agree.

(I just noticed that AMD uses this convention as well, by the way.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Absolute vs Relative Imports in Python
An absolute import specifies the resource to be imported using its full path from the project's root folder. Syntax and Practical Examples. Let's...
Read more >
What is the difference between an absolute and a relative ...
These paths are absolute because they are non ambiguous. Example 1 shows an absolute file path, and example 2 shows an absolute URL....
Read more >
Importing with Absolute Paths in JavaScript/TypeScript using ...
Relative paths aren't entirely bad. For example, when importing a closely related file, something that would be considered part of the same ...
Read more >
What's wrong with relative imports in Python?
Python 3 has disabled implicit relative imports altogether; imports are now always interpreted as absolute, meaning that in the above example ...
Read more >
Import Path Resolution — Solidity 0.8.16 documentation
Relative imports, where you specify a path starting with ./ or ../ to be combined with the source unit name of the importing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found