Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

stringifyRequest - wrong relative path #115

@jantimon

Description

@jantimon

Source:
a failed unit test of the html-webpack-plugin webpack 4 branch

Error:
Module not found: Error: Can't resolve '../../home/travis/build/jantimon/html-webpack-plugin/node_modules/lodash/lodash.js' in '/home/travis/build/jantimon/html-webpack-plugin/spec/fixtures':

Description:
stringifyRequest does always transform an absolute path to a relative one based on the loader context. Unfortunately the loader context is sometimes different from the compiler context.

singlePath = path.relative(context, singlePath);

Loader Context:

The reason for the loader context not to be the same as the compiler context is a resolve plugin which manipulates the context to resolve a modules dependencies properly:

    compiler.hooks.normalModuleFactory.tap(
      "ChangeModuleResolution",
      nmf => {
        nmf.hooks.beforeResolve.tapAsync(
          "ChangeModuleResolution",
          (data, callback) => {
            if(someCondition) {
               data.context = '/a/path/';
            }
            return callback(null, this._resolveDependency(data));
          }
        );
      }
    );

Loader Code:

console.log({
    loaderContext: this.context,
    compilerContext: this._compiler.context,
    resolve: require.resolve('lodash'),
    loaderUtils: loaderUtils.stringifyRequest(this, '!!' + require.resolve('lodash'))
  });
{ cwd: '/home/travis/build/jantimon/html-webpack-plugin',
  loaderContext: '/tmp/tmp-1529996640938-WMgSVD1rkurD',
  compilerContext: '/home/travis/build/jantimon/html-webpack-plugin/spec',
  resolve:
   '/home/travis/build/jantimon/html-webpack-plugin/node_modules/lodash/lodash.js',
  loaderUtils:
   '"!!../../home/travis/build/jantimon/html-webpack-plugin/node_modules/lodash/lodash.js"' }

Result from: https://travis-ci.org/jantimon/html-webpack-plugin/jobs/396738867

errorDetails:

{ id: './lib/loader.js!../../../../../tmp/tmp-1530024669301-m1fuknBuohSp/plain.html',
            identifier: '/home/travis/build/jantimon/html-webpack-plugin/lib/loader.js!/tmp/tmp-1530024669301-m1fuknBuohSp/plain.html',
            name: './lib/loader.js!/tmp/tmp-1530024669301-m1fuknBuohSp/plain.html',
            ....
            source: 'var _ = require("!!../../home/travis/build/jantimon/html-webpack-plugin/node_modules/lodash/lodash.js");module.exports = function (templateParams) { ...}}' } ],

Full log and trace is here: https://travis-ci.org/jantimon/html-webpack-plugin/jobs/396925410

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions