Skip to content

fix: should not fail when output directory is the root directory#10545

Open
haoqunjiang wants to merge 5 commits into
webpack:mainfrom
haoqunjiang:mkdirp-root
Open

fix: should not fail when output directory is the root directory#10545
haoqunjiang wants to merge 5 commits into
webpack:mainfrom
haoqunjiang:mkdirp-root

Conversation

@haoqunjiang

Copy link
Copy Markdown
Contributor

Fixes #10544.

Though the EISDIR error code is not mentioned in the POSIX standard,
FreeBSD and memfs both throw this error when trying to run mkdir on
/. So essentially we should treat it the same as EEXIST.

References:

What kind of change does this PR introduce?

A bugfix

Did you add tests for your changes?

I'm not sure how I should add tests, because this issue only happens on BSD systems.

Does this PR introduce a breaking change?

No

What needs to be documented once your changes are merged?

Nothing

Fixes webpack#10544.

Though the `EISDIR` error code is not mentioned in the POSIX standard,
FreeBSD and memfs both throws this error when trying to run `mkdir` on
`/`. So essentially we should treat it the same as `EEXIST`.

References:
* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=59739
* streamich/memfs#326
@webpack-bot

Copy link
Copy Markdown
Contributor

For maintainers only:

  • This needs to be documented (issue in webpack/webpack.js.org will be filed when merged)
  • This needs to be backported to webpack 4 (issue will be created when merged)

@alexander-akait alexander-akait left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add test


module.exports = {
findBundle: function() {
return [path.relative(outputDirectory, "/tmp/bundle.js")];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I return an absolute path here directly, the _require function in ConfigTestCases.test.js would require the bundle directly, making it lack of essential context utilities like the it function.

So the only workaround I found is to calculate the relative path of /tmp/bundle.js.

@alexander-akait

Copy link
Copy Markdown
Member

/cc @sodatea friendly ping

@haoqunjiang

Copy link
Copy Markdown
Contributor Author

I think I need to work out a more generic way to deal with the root directory case, instead of relying on the error code because different OSes😅
I'll try to fix it this weekend.

@haoqunjiang haoqunjiang changed the title fix: also check for "EISDIR" in mkdirp fix: should not fail when output directory is the root directory Jul 14, 2020
Comment thread lib/util/fs.js Outdated
* @returns {void}
*/
const mkdirp = (fs, p, callback) => {
if (path.resolve(p) === path.resolve("/")) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need so many path.resolve? They have a performance cost. I think p is always a absolute path anyway. What is about windows absolute paths?

Suggested change
if (path.resolve(p) === path.resolve("/")) {
if (/^(\/|[A-Za-z]:\\?|\\\\[^\\]+\\?)$/.test(p)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail edge cases like multiple trailing backslashes (such as C:\\\\).

Though, we can avoid the performance overhead by moving the logic into the error handling part.

@webpack-bot

Copy link
Copy Markdown
Contributor

@sodatea Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@sokra Please review the new changes.

@webpack-bot

Copy link
Copy Markdown
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

webpack 5 can't set output path to /

4 participants