nft
nft copied to clipboard
Different behavior for same code: Single app & Monorepo
I'm working with monorepo of Next.js apps + nx
and found some strange behavior, the file next-i18next.config.js
(from the next-i18next
package), which is used by an application, doesn't apply to the build.
So I did some research and found that behavior for the package @vercel/nft
is different when we run a script with the package to analyze files. I have written a small package with the same idea of files loading as the next-i18next
package to show the issue and a few examples which are using the package.
Script
const path = require('path');
const { nodeFileTrace } = require('@vercel/nft');
(async () => {
const files = [path.resolve(__dirname, 'app.js')];
const { fileList } = await nodeFileTrace(files);
console.log('fileList', fileList);
})();
Single application/package project
npm run files
> [email protected] files
> node nft.js
fileList Set(6) {
'app.js',
'package.json',
'node_modules/intpp-some-nft-demo/package.json',
'node_modules/intpp-some-nft-demo/index.js',
'config.js',
'assets/common.json'
}
Monorepo from a project's root
npm run files:app1
> [email protected] files:app1
> node apps/app1/nft.js
fileList Set(4) {
'apps/app1/app.js',
'apps/app1/package.json',
'node_modules/intpp-some-nft-demo/package.json',
'node_modules/intpp-some-nft-demo/index.js'
}
Monorepo from an application's root
npm run files
> [email protected] files
> node nft.js
fileList Set(2) { 'app.js', 'package.json' }
Path for the file app.js
the same.
Demo repos: https://github.com/intpp/vercel-nft-demo https://github.com/intpp/vercel-nft-workspace-demo
Isn't that because of the location-dependent behaviours?
From the readme:
Process Cwd
When applying analysis certain functions rely on the process.cwd() value, such as path.resolve('./relative') or even a direct process.cwd() invocation.
Setting the processCwd option allows this analysis to be guided to the right path to ensure that assets are correctly detected.
const { fileList } = await nodeFileTrace(files, {
processCwd: path.resolve(__dirname)
}
By default processCwd is the same as base.
Probably yes, but I think it shouldn't affect detecting correct files because the same target file still has the same dependencies. Am I wrong?
I have updated monorepo's examples with the options base
and processCwd
.
Next.js has "experimental.outputFileTracingRoot" and it still doesn't work perfectly for monorepos because of this issue...
it shouldn't affect detecting correct files because the same target file still has the same dependencies. Am I wrong?
I think you're right, but this line seems to imply that the files it detects might be dependent on the cwd setting:
Setting the processCwd option allows this analysis to be guided to the right path to ensure that assets are correctly detected.
If that's the case, then we probably need to find the magical combination of callsite + cwd settings to give expected behaviour in any scenario.
I'm really interested in seeing you succeed with this challenge for my own use-cases. Does the update you made to your examples give any different results to your initial reports?