Skip to content

Conversation

icyJoseph
Copy link
Contributor

In this input file:

const fs = require('fs');

fs.readFile('./asset1.txt')

fs.readFile(require.resolve('./asset2.txt'))

const _basePath = __dirname;
const asset3 = 'asset3.txt';
fs.readFileSync(_basePath + '/' + asset3, 'utf8');

We don't emit for fs.readFile('./asset1.txt'), but we should:

Regardless of where the code is (i.e. in which chunk it ends up in after bundling), readFile is always relative to process.cwd() which is set by Next.js to always be the project root (otherwise process.cwd() + "./foo" wouldn't work either

@icyJoseph icyJoseph requested review from ijjk, styfle and a team as code owners September 24, 2025 22:02
src/analyze.ts Outdated
typeof staticChildValue.value === 'string' &&
staticChildValue.value.startsWith('.')
) {
staticChildValue.value = path.resolve(dir, staticChildValue.value);
Copy link
Member

Choose a reason for hiding this comment

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

Are we matching behavior here? vercel/next.js#84155 seems like this is diverging

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmm - yeah, I have it wrong here, I should use cwd - and update the test

Copy link
Contributor Author

@icyJoseph icyJoseph Sep 24, 2025

Choose a reason for hiding this comment

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

if we have:

├── hello.json
├── index.js
├── lib
│   └── index.js
└── package.json

With lib/index.js as:

const fs = require("fs/promises")

fs.readFile('./hello.json', 'utf-8').then(console.log)

And index.js as:

require("./lib")

Then node index.js resolves hello.json in lib from the cwd, not the lib/index.js file. Also follows that node lib/index.js resolves from the root too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this branch, running npm run build && node out/cli.js print test/unit/fs-emission/input.js won't output asset1.txt cuz it doesn't exist in the root.

If we place a dummy asset1.txt at the root and re-run, then we trace the file.

Copy link
Member

Choose a reason for hiding this comment

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

I think we have two variations one with cwd at the root and once in the fixture

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, both tests pass with that condition - I shared that as an example of how to test manually.

@icyJoseph icyJoseph changed the title fix: fs.readFile emit relative assets fix: fs.readFile emit relative assets using cwd Sep 24, 2025
Copy link
Member

@styfle styfle left a comment

Choose a reason for hiding this comment

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

Great work, thanks! 🥇

@styfle styfle enabled auto-merge (squash) September 26, 2025 16:57
@styfle styfle merged commit dc4ab0b into main Sep 26, 2025
12 checks passed
@styfle styfle deleted the fix/emit-static-child-asset-for-relative-paths branch September 26, 2025 17:03
Copy link

🎉 This PR is included in version 0.30.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

3 participants