Skip to content

Commit 5fde543

Browse files
authored
fix: resolve site dependency version from packagePath if defined (#6650)
1 parent d86f532 commit 5fde543

File tree

10 files changed

+117
-5
lines changed

10 files changed

+117
-5
lines changed

packages/build/src/plugins/plugin_conditions.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const siteDependenciesTest = async function (
5252
return (
5353
await Promise.all(
5454
Object.entries(allowedSiteDependencies).map(async ([dependencyName, allowedVersion]) =>
55-
siteDependencyTest({ dependencyName, allowedVersion, siteDependencies, buildDir }),
55+
siteDependencyTest({ dependencyName, allowedVersion, siteDependencies, buildDir, packagePath }),
5656
),
5757
)
5858
).every(Boolean)
@@ -63,10 +63,12 @@ const siteDependencyTest = async function ({
6363
allowedVersion,
6464
siteDependencies,
6565
buildDir,
66+
packagePath,
6667
}: {
6768
dependencyName: string
6869
allowedVersion: string
6970
buildDir: string
71+
packagePath?: string
7072
siteDependencies: Record<string, string | undefined>
7173
}): Promise<boolean> {
7274
const siteDependency = siteDependencies[dependencyName]
@@ -81,7 +83,7 @@ const siteDependencyTest = async function ({
8183

8284
try {
8385
// if this is a range we need to get the exact version
84-
const packageJsonPath = await resolvePath(`${dependencyName}/package.json`, buildDir)
86+
const packageJsonPath = await resolvePath(`${dependencyName}/package.json`, join(buildDir, packagePath ?? ''))
8587
const { version } = await importJsonFile(packageJsonPath)
8688
if (!version) {
8789
return false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[plugins]]
2+
package = "netlify-plugin-contextual-env"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "module_plugin",
3+
"version": "0.0.1",
4+
"type": "module",
5+
"description": "test",
6+
"license": "MIT",
7+
"repository": "test",
8+
"dependencies": {
9+
"@netlify/dependency-with-range": "^9.5.4"
10+
}
11+
}

packages/build/tests/plugins_list/fixtures/plugins_compat_site_dependencies_range_monorepo_hoisted/node_modules/@netlify/dependency-with-range/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"workspaces": [
3+
"apps/*"
4+
]
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[[plugins]]
2+
package = "netlify-plugin-contextual-env"

packages/build/tests/plugins_list/fixtures/plugins_compat_site_dependencies_range_monorepo_not_hoisted/apps/web/node_modules/@netlify/dependency-with-range/package.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "module_plugin",
3+
"version": "0.0.1",
4+
"type": "module",
5+
"description": "test",
6+
"license": "MIT",
7+
"repository": "test",
8+
"dependencies": {
9+
"@netlify/dependency-with-range": "^9.5.4"
10+
}
11+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"workspaces": [
3+
"apps/*"
4+
]
5+
}

packages/build/tests/plugins_list/tests.js

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import cpy from 'cpy'
88

99
const FIXTURES_DIR = fileURLToPath(new URL('fixtures', import.meta.url))
1010

11-
const runWithApiMock = async function (
12-
t,
11+
const runWithApiMockAndGetNormalizedOutput = async function (
1312
fixtureName,
1413
{ testPlugin, response = getPluginsList(testPlugin), ...flags } = {},
1514
status = 200,
@@ -26,12 +25,21 @@ const runWithApiMock = async function (
2625
...flags,
2726
})
2827
.runWithBuild()
29-
await t.snapshot(normalizeOutput(output))
28+
return normalizeOutput(output)
3029
} finally {
3130
await stopServer()
3231
}
3332
}
3433

34+
const runWithApiMock = async function (
35+
t,
36+
fixtureName,
37+
{ testPlugin, response = getPluginsList(testPlugin), ...flags } = {},
38+
status = 200,
39+
) {
40+
await t.snapshot(await runWithApiMockAndGetNormalizedOutput(fixtureName, { testPlugin, response, ...flags }, status))
41+
}
42+
3543
// We use a specific plugin in tests. We hardcode its version to keep the tests
3644
// stable even when new versions of that plugin are published.
3745
const getPluginsList = function (testPlugin = DEFAULT_TEST_PLUGIN) {
@@ -289,6 +297,66 @@ test.serial('Plugins can specify non-matching compatibility.siteDependencies ran
289297
})
290298
})
291299

300+
test.serial(
301+
'Plugins can specify matching compatibility.siteDependencies range in monorepo with hoisted node_modules',
302+
async (t) => {
303+
await removeDir(`${FIXTURES_DIR}/plugins_compat_site_dependencies_range_monorepo_hoisted/apps/web/.netlify`)
304+
const normalizedOutput = await runWithApiMockAndGetNormalizedOutput(
305+
'plugins_compat_site_dependencies_range_monorepo_hoisted',
306+
{
307+
testPlugin: {
308+
compatibility: [
309+
{ version: '0.3.0' },
310+
{
311+
version: '0.2.0',
312+
siteDependencies: {
313+
// this is satisfied, so this version should be selected
314+
'@netlify/dependency-with-range': '<10',
315+
},
316+
},
317+
],
318+
},
319+
packagePath: 'apps/web',
320+
},
321+
)
322+
t.true(
323+
normalizedOutput.includes(
324+
'netlify-plugin-contextual-env 0-2-0 from netlify.toml (latest 0-3-0, expected 0-2-0, compatible 0-2-0)',
325+
),
326+
)
327+
},
328+
)
329+
330+
test.serial(
331+
'Plugins can specify matching compatibility.siteDependencies range in monorepo without hoisted node_modules',
332+
async (t) => {
333+
await removeDir(`${FIXTURES_DIR}/plugins_compat_site_dependencies_range_monorepo_not_hoisted/apps/web/.netlify`)
334+
const normalizedOutput = await runWithApiMockAndGetNormalizedOutput(
335+
'plugins_compat_site_dependencies_range_monorepo_not_hoisted',
336+
{
337+
testPlugin: {
338+
compatibility: [
339+
{ version: '0.3.0' },
340+
{
341+
version: '0.2.0',
342+
siteDependencies: {
343+
// this is satisfied, so this version should be selected
344+
'@netlify/dependency-with-range': '<10',
345+
},
346+
},
347+
],
348+
},
349+
packagePath: 'apps/web',
350+
},
351+
)
352+
t.true(
353+
normalizedOutput.includes(
354+
'netlify-plugin-contextual-env 0-2-0 from netlify.toml (latest 0-3-0, expected 0-2-0, compatible 0-2-0)',
355+
),
356+
)
357+
},
358+
)
359+
292360
test.serial('Plugin versions can be feature flagged', async (t) => {
293361
await removeDir(`${FIXTURES_DIR}/plugins_compat_node_version/.netlify`)
294362
await runWithApiMock(t, 'plugins_compat_node_version', {

0 commit comments

Comments
 (0)