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

CLI: SourceMap sources path should be relative #3296

Closed
am11 opened this issue Dec 29, 2013 · 10 comments
Closed

CLI: SourceMap sources path should be relative #3296

am11 opened this issue Dec 29, 2013 · 10 comments
Labels

Comments

@am11
Copy link

am11 commented Dec 29, 2013

Relative to the location of compiled map and .coffee or absolute URL on disk, when compiled with Node.

Consider the following structure:

website_root
        \
        assets
        \
        coffee
               \
                work.coffee
        \
        javascript
               \
                 work.js
               \
                 work.js.map

It was result of this command:

> node "node_modules\iced-coffee-script\bin\coffee"
  --bare --runtime inline --map
  --output "C:\projects\test\website_root\javascript\" 
  --compile  "C:\projects\test\website_root\coffee\work.coffee"

Now, the sources[] inside the .map file looks like:

"sources": [
  "worker.coffee"
],

While expected result is:

relative path

"sources": [
  "../coffee/worker.coffee"
],

or absolute path

"sources": [
  "c:\projects\test\website_root\coffee\worker.coffee"
],

or optional switch for either:

Note: LESS provides --source-map-basepath={relative or absolute path} argument and you can supply desired path to .map file as well. That might worth checking out in this situation.


Proposed Solution:

LESS provides six compiler arguments to deal with source map in general, out of which four effects on file paths, viz;

  --source-map[=FILENAME]      # Outputs a v3 sourcemap to the filename (or output filename.map)
  --source-map-rootpath=X         # adds this path onto the sourcemap filename and less file paths
  --source-map-basepath=X       # Sets sourcemap base path, defaults to current working directory.
  --source-map-url=URL             # the complete url and filename put in the less file
  --source-map-less-inline          # puts the less files into the map instead of referencing them
  --source-map-map-inline         # puts the map (and any less files) into the output css file

(first four)

I personally find it very confusing and one may not get it unless experiment with it for a while.

Having said that, considering our aim is to keep the end result consistent, that is; the related .coffee, .map and .js can be saved at different locations, we can fix it by providing "value" in existing compiler switch; --map[=FileNameWithPath]. After that, calculate the following relative paths:

  • from .css to .map; for sourceMap comment in CSS file.
  • from .map to .css; for the value of "file" member / property* in .map JSON.
  • from .map to each .less file; for sources array in .map JSON.

This is how we calculated relative paths in .NET independent of FileSystem API. We can write a similar method in JavaScript.

For Windows, there is C++ file system function to resolve relative paths; PathRelativePathTo() in shlwapi.dll. I am sure for other OS and languages, there must be a similar low-level implementation to calculate the shortest path between two files on disc.

For further references please check the LESS implementation and see this discussion for various approaches to solve relative URL issues: madskristensen/WebEssentials2013#134.

@michaelficarra
Copy link
Collaborator

@am11: It looks like you're using iced-coffee-script here. Can you confirm that this bug exists on jashkenas/coffee-script@master as well?

@am11
Copy link
Author

am11 commented Dec 30, 2013

@michaelficarra, Yes. I installed npm using npm install coffee-script, and the same bug is reproducible with vanilla coffee-script compiler. Just remove iced- and --runtime inline from the aforementioned reproduction steps:

> node "node_modules\coffee-script\bin\coffee"
  --bare --map
  --output "C:\\projects\test\website_root\javascript\" 
  --compile  "C:\\projects\test\website_root\coffee\work.coffee"

am11 referenced this issue in am11/WebEssentials2013 Jan 1, 2014
@am11
Copy link
Author

am11 commented Jan 1, 2014

@michaelficarra, I have added proposed solution. Hopefully, it will help you guys in some way. :)

@jwalton
Copy link

jwalton commented Jan 29, 2014

Agreed; this is a problem.

@joliss
Copy link
Contributor

joliss commented Feb 5, 2015

FYI, we're trying to get source maps to "just work" in the CoffeeScript plugin for the Broccoli build tool (joliss/broccoli-coffee#9), and this bug is blocking us. Would love to see this fixed!

@jashkenas
Copy link
Owner

Let's fix it!

@am11
Copy link
Author

am11 commented Feb 6, 2015

Yay!

@Hubro
Copy link

Hubro commented Nov 10, 2015

I just had to do some pretty awkward stuff in my coffee build script (involving copying my whole source directory into my static js folder and changing working directory) to get the source mapping feature to work. Being able to override the root path and base path would have saved me a couple of headaches.

Has there been any progress in that regard?

@GeoffreyBooth GeoffreyBooth changed the title SourceMap sources path should be relative CLI: SourceMap sources path should be relative May 6, 2017
@GeoffreyBooth
Copy link
Collaborator

See also #3075.

@GeoffreyBooth
Copy link
Collaborator

I just tested this, and I’m unable to reproduce (which isn’t all that surprising, as it’s four years later):

➜ coffee -v
CoffeeScript version 2.0.0-beta4
➜ mkdir -p website_root/assets website_root/coffee website_root/js
➜ echo 'console.log Date.now()' > ./website_root/coffee/work.coffee
➜ coffee --bare --map --output ./website_root/js --compile ./website_root/coffee/work.coffee
➜ cat ./website_root/js/work.js.map
{
  "version": 3,
  "file": "work.js",
  "sourceRoot": "../..",
  "sources": [
    "website_root/coffee/work.coffee"
  ],
  "names": [],
  "mappings": ";AAAA,OAAO,CAAC,GAAR,CAAY,IAAI,CAAC,GAAL,CAAA,CAAZ"
}

Note the relative path in the sources array. It could also be that something in Node’s fs module changed to fix this; I’m running Node 8.4.0. I’m also on OS X, in case perhaps this was a Windows issue (or perhaps a Windows issue in an older version of Node).

If this is still an issue and someone can write some steps to reproduce, please post those steps in a comment and I’ll reopen.

At this point we don’t plan to add any more command-line flags, so there won’t be an option to get sources output as absolute paths. Creating a build script to convert these relative paths to absolute ones, though, shouldn’t be difficult. Here’s an example from Coffeeify rewriting the sources field.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants