Skip to content

Commit fe68c74

Browse files
authored
fix: fixes an issue where the nft tracing was not picking up the runtime node_modules (#74)
* chore: log nft file list * chore: log metadata * chore: update * chore: update * chore: fix * chore: update * chore: fix * chore: update * chore: final fix * chore: cleanup
1 parent 80b5ea9 commit fe68c74

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netlify/next-runtime",
3-
"version": "5.0.0-alpha.7",
3+
"version": "5.0.0-alpha.19",
44
"description": "Run Next.js seamlessly on Netlify",
55
"main": "./dist/index.js",
66
"type": "module",

src/build/functions/server.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NetlifyPluginOptions } from '@netlify/build'
22
import { nodeFileTrace } from '@vercel/nft'
33
import { cp, rm, writeFile } from 'fs/promises'
4-
import { join, resolve } from 'node:path'
4+
import { join, relative, resolve } from 'node:path'
55
import {
66
PLUGIN_DIR,
77
PLUGIN_NAME,
@@ -27,13 +27,27 @@ export const createServerHandler = async ({
2727
join(PLUGIN_DIR, 'dist/run/handlers/cache.cjs'),
2828
join(PLUGIN_DIR, 'dist/run/handlers/next.cjs'),
2929
],
30-
{ base: PLUGIN_DIR, ignore: ['package.json', 'node_modules/next/**'] },
30+
// base in this case is the directory where it should stop looking up.
31+
// by setting this to `/` we get absolute paths and don't have to worry about wrongly chaining it
32+
{
33+
base: '/',
34+
ignore: ['**/node_modules/next/**'],
35+
},
3136
)
3237

3338
// copy the handler dependencies
3439
await Promise.all(
3540
[...fileList].map(async (path) => {
36-
await cp(resolve(PLUGIN_DIR, path), resolve(SERVER_HANDLER_DIR, path), { recursive: true })
41+
// we have to use a fake cwd that points to the actual repository root. On the deployed version this will be the build directory
42+
// As in the integration tests the node_modules are not located in the tmp directory
43+
const cwd = process.env.NETLIFY_FAKE_TEST_CWD || process.cwd()
44+
const absPath = `/${path}`
45+
// if the file that got traced is inside the plugin directory resolve it with the plugin directory
46+
// if it is a node_module resolve it with the process working directory.
47+
const relPath = relative(path.includes(PLUGIN_NAME) ? PLUGIN_DIR : cwd, absPath)
48+
await cp(absPath, resolve(SERVER_HANDLER_DIR, relPath), {
49+
recursive: true,
50+
})
3751
}),
3852
)
3953

tests/utils/fixture.ts

+5
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ export async function runPlugin(
9595
ctx: FixtureTestContext,
9696
constants: Partial<NetlifyPluginConstants> = {},
9797
) {
98+
// this is needed as the node_modules of the runtime are not copied over to the temp directory
99+
// so in this case we use the NETLIFY_FAKE_TEST_CWD || process.cwd() that will fallback to the actual cwd on production
100+
const repoRoot = fileURLToPath(new URL('../../', import.meta.url))
101+
vi.stubEnv('NETLIFY_FAKE_TEST_CWD', repoRoot)
102+
98103
const { onBuild } = await import('../../src/index.js')
99104
await onBuild({
100105
constants: {

0 commit comments

Comments
 (0)