vercel / vercel/ncc

Build creating nested dists

Open
#714 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
9.8k
Forks
326
PR merge metrics
No merged PRs in 30d

Description

Description

This is a weird one, fellas.

A little background: I'm using ncc to build several GitHub Actions that are used for a deploy job. One of those actions needs to read in files from a /dist built through Webpack and then upload them to an AWS S3 bucket.

Behavior

The build output is creating nested dist files in each existing dist when there is a matching string "dist" in the script.

Here are examples of the two functions in my script which seem to be causing the issue:

const getFiles = () => {
  try {
    return fs.readdirSync(path.join(process.cwd(), 'dist'));
  } catch(e) {
    setFailed(`Failed to read dist: ${e}`);
  }
}

const readFile = (filePath) => {
  try {
    const absolutePath = path.join(process.cwd(), `dist/${filePath}`);
    info(`Reading file from ${absolutePath}`)
    return fs.readFileSync(absolutePath);
  } catch(e) {
    setFailed(`Failed to read file ${filePath}: ${e}`);
  }
}

Now, here's where things get weird. When I run ncc build (with the default /dist output) or ncc build -o dist, the build generates nested build directories inside of each build for each subsequent execution of the command, like:

|- dist
|-- dist
|--- index.js
|-- index.js
...

This will continue to generate nested build directories every time you run ncc build.

In the build output, it also converts the aforementioned functions getFiles and readFile in the following way, which may give some clue as to what is happening(?):

const getFiles = () => {
  try {
    return external_fs_.readdirSync(__nccwpck_require__.ab + "dist");
  } catch(e) {
    setFailed(`Failed to read dist: ${e}`);
  }
}

const readFile = (filePath) => {
  try {
    const absolutePath = __nccwpck_require__.ab + "dist/" + filePath;
    info(`Reading file from ${absolutePath}`)
    return external_fs_.readFileSync(absolutePath);
  } catch(e) {
    setFailed(`Failed to read file ${filePath}: ${e}`);
  }
}

This does not happen when I simply read from a directory with a different name. I have also tested with names other than dist and as long as the directory in the script matches the build output then this behavior manifests itself.

Workaround

To work around this, I have simply changed the output target to something like ncc build -o build and the issue goes away.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the shown ncc build commands with a source script that reads from a directory matching the output name, and compare them with the alternate build target. Trace the build entry point responsible for resolving that directory; done means repeated builds no longer create nested output directories while the generated bundle still reads the intended files.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, javascript, node.js, webpack
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.