-
-
Notifications
You must be signed in to change notification settings - Fork 209
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
Corrupt source map paths on Windows and invalid sources
#390
Comments
@rjgotten can you create minimum reproducible test repo? |
@evilebottnawi |
@rjgotten Could you try with the following patch ? https://github.com/postcss/postcss-loader/blob/master/src/index.js#L157-L159 if (map) {
- map.file = path.isAbsolute(map.file) ? map.file : path.resolve(map.file)
+ map.file = path.isAbsolute(map.file) ? map.file : path.resolve(map.file)
- map.sources = map.sources.map((src) => path.resolve(src))
+ map.sources = map.sources.map((src) => {
+ if (path.isAbsolute(src))
+ return src
+ }
+
+ return path.resolve(src)
+ })
} |
@michael-ciniawsky good catch! Don't think what source map can have absolute path 😕 |
This seems to be likely the offending line causing the issue on windows when the
{
version: 3,
sources: [
'/Users/Cini/Github/webpack/__test__/client/styles/imports/import.css',
'/Users/Cini/Github/webpack/__test__/client/styles/App.scss'
],
names: [],
mappings: 'AAAA,yBAAA;ACAA;EACE,...',
file: '/Users/Cini/Github/webpack/__test__/client/styles/App.scss',
sourcesContent: [ '...' ]
} |
@michael-ciniawsky what do you think about this https://github.com/webpack-contrib/sass-loader/blob/master/lib/loader.js#L70? Maybe we should fix it in |
Sadly has no effect. I'll start working on building a test case. |
@evilebottnawi The build has a passthrough loader woven in which shows the source map sources going into and coming out of postcss loader. What I'm seeing when running this on Windows:
|
Did you also try with commenting out these lines completely to see if the |
I think Microsoft offers VMs for developers, with licenses that auto-expire after a set period.
Hmm.. I should've remembered to try that. Ok. I've made a note to try commenting out the |
I once found there is a key function ( but after discussing, I realized that the paths in the sources field should be URLs but the os paths, that why util.join() doesn't treat windows device path as an absolute path. so it generated the paths like this. I read the code of the build pipeline: [css-loader, postcss-loader, sass-loader], then I got many bad idea in them: sass-loader: normalize all the relative path in the sources field to os path, bad! since it should be URLs, should always use POSIX path separator (
There will be much effort to fix the bug since it's related to 3 loaders & 1 lib, so I wrote a small loader to fix the sourcemap temporary in my projects. Which fix and convert the os absolute path to |
Imho all loaders should be producing source maps that are based on relative URIs from generated source to original source. Any loader that doesn't generate source maps in that way is - quite frankly - "doing it wrong" and should be fixed. It should be Webpack's responsibility to remap those relative paths to the |
I think it does that already, but later (after loaders) within a webpack (core) plugin ( |
And here's the likely root cause of all these loaders having bad implementations: the fact that Webpack requires intentionally malformed source maps to work 'correctly' to begin with. If it all winds back to Webpack itself, then they need a good boot up the ass to fix this. The fact that it "seems to be also working on UNIX platforms" is probably why this problem came to be so wide-spread and persistent in the first place. Odds are someone half-assed the core source mapping functionality and never bothered looking at it any further, because "works on my machine". i.e. it works under their *nix OS, where OS paths happily are close enough to URIs to be compatible. Not so with Windows, ofcourse. Lord knows why Webpack's core source maps plugin requires absolute file paths. It has access to Webpack's own output paths as the generated source paths, which it should already be able to combine with relative paths in the source map just fine. And that should give it the absolute paths it needs to produce its project-relative |
https://github.com/webpack/webpack/blob/master/lib/SourceMapDevToolPlugin.js#L174-L223 I don't know the exact why behind it, |
@michael-ciniawsky Should have very little, if anything, to do with the types it can take in. Which should just be spec-adherent. |
Somebody can create minimum reproducible test repo? |
You missed this, maybe? |
Very thanks, we work on refactor |
I got this issue with invalid source-map paths in Windows too. I use this loader chain: In generated map file i get this source file paths: When i disable postcss-loader, then i get source map files with normal paths: |
@syberon can you create minimum reproducible test repo? |
The problem is caused by Webpack's build pipeline being entirely based on local filesystem paths, including paths used for source maps, whereas source maps by specification require valid URIs. The official source maps reference implementation from Mozilla holds to the specification and will not treat Webpack itself dodges that bullet because it rolls its own source mapping implementation, which plays fast-and-loose with the specification on this point. But PostCSS and everything that makes use of it -- that includes The architecturally sound solution here is for Webpack to fix their broken non-compliant implementation, but they've been pathologically in denial about it being an issue for 2+ years. So you're left with each individual loader that (even transitively) makes use of the reference implementation from the You can handle this transformation yourself externally, via a few custom loaders that manipulate the source maps. For your specific case:
NOTE: You cannot transform the source map paths back before <rant> |
@evilebottnawi Main problem is because of one backslash inserted in path (before @rjgotten
|
@syberon const path = require( "path" );
module.exports = function( source, map ) {
if ( map && map.sources ) {
let context = this.context.replace( /\\/g, "/" );
map.sources = map.sources.map( source => {
if ( !path.isAbsolute( source )) return source;
source = source.replace( /\\/g, "/" );
return path.posix.relative( context, source );
});
}
this.callback( null, source, map );
}; And then put something like the following between const path = require( "path" );
const esprima = require( "esprima" );
const escodegen = require( "escodegen" );
module.exports = function( content ) {
this.callback( null, adaptSources( content, this.context ));
};
function adaptSources( content, context ) {
let ast = esprima.parseScript( content, { comment : true }, node => {
if ( !isSourceMap( node )) return;
getSourcesNodes( node ).forEach( node => {
if ( !path.isAbsolute( node.value )) {
node.value = path.resolve( context, node.value );
}
});
});
return escodegen.generate( ast );
}
function isSourceMap( node ) {
if ( node.type !== "ObjectExpression" ) return false;
let first = node.properties && node.properties[ 0 ];
return (
first != null
&& first.type === "Property"
&& first.key.type === "Literal"
&& first.key.value === "version"
);
}
function getSourcesNodes( node ) {
let prop = node.properties.find( prop => prop.key.value === "sources" );
return prop && ( prop.value.type === "ArrayExpression" )
? prop.value.elements
: [];
} That should provide a more general purpose, robust |
Somebody can create reproducible test repo, we will update scss/less/css loaders in near future, so we can try to fix it |
@evilebottnawi |
@syberon big thanks, i will loon on this problem in near future ⭐ |
Hi. Is this issue resolved? |
sources
Fixed in master, release will be soon - next week |
Details
postcss-loader continues to produce invalid source map paths on Windows, after #224 was closed.
Said issue was pointing to css-loader as a culprit.
css-loader was fixed.
postcss-loader was not.
Reproduction (Code)
The following webpack loader sequence produces valid source map paths:
The following webpack loader sequences produces INVALID paths:
I have also written a small custom loader which passes through the content and sourcemap from the previous loader, while logging the sourcemap
sources
array with a simpleconsole.log()
call.Placing said logger on top of sass-loader shows paths relative to the webpack project root. Placing said logger on top of postcss-loader shows the same type of corrupted paths as mentioned in #224.
Environment
The text was updated successfully, but these errors were encountered: