[fs-detectors] Fix turbo@1.8 all filters are inferred (#9680)

We no longer need to specify the active folder for `turbo` to identify the context correctly.
This commit is contained in:
Nathan Hammond
2023-03-31 21:02:30 +00:00
committed by GitHub
parent c29f2b2fbd
commit 10d14488db
5 changed files with 60 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import { packageManagers } from '../package-managers/package-managers';
import { DetectorFilesystem } from '../detectors/filesystem'; import { DetectorFilesystem } from '../detectors/filesystem';
import { detectFramework } from '../detect-framework'; import { detectFramework } from '../detect-framework';
import JSON5 from 'json5'; import JSON5 from 'json5';
import semver from 'semver';
export class MissingBuildPipeline extends Error { export class MissingBuildPipeline extends Error {
constructor() { constructor() {
@@ -52,6 +53,7 @@ export async function getMonorepoDefaultSettings(
]); ]);
let hasBuildPipeline = false; let hasBuildPipeline = false;
let turboSemVer = null;
if (turboJSONBuf !== null) { if (turboJSONBuf !== null) {
const turboJSON = JSON5.parse(turboJSONBuf.toString('utf-8')); const turboJSON = JSON5.parse(turboJSONBuf.toString('utf-8'));
@@ -59,12 +61,19 @@ export async function getMonorepoDefaultSettings(
if (turboJSON?.pipeline?.build) { if (turboJSON?.pipeline?.build) {
hasBuildPipeline = true; hasBuildPipeline = true;
} }
} else if (packageJSONBuf !== null) { }
if (packageJSONBuf !== null) {
const packageJSON = JSON.parse(packageJSONBuf.toString('utf-8')); const packageJSON = JSON.parse(packageJSONBuf.toString('utf-8'));
if (packageJSON?.turbo?.pipeline?.build) { if (packageJSON?.turbo?.pipeline?.build) {
hasBuildPipeline = true; hasBuildPipeline = true;
} }
turboSemVer =
packageJSON?.dependencies?.turbo ||
packageJSON?.devDependencies?.turbo ||
null;
} }
if (!hasBuildPipeline) { if (!hasBuildPipeline) {
@@ -74,17 +83,25 @@ export async function getMonorepoDefaultSettings(
if (projectPath === '/') { if (projectPath === '/') {
return { return {
monorepoManager: 'turbo', monorepoManager: 'turbo',
buildCommand: 'npx turbo run build', buildCommand: 'turbo run build',
installCommand: packageManager ? `${packageManager} install` : null, installCommand: packageManager ? `${packageManager} install` : null,
commandForIgnoringBuildStep: 'npx turbo-ignore', commandForIgnoringBuildStep: 'npx turbo-ignore',
}; };
} }
let buildCommand = null;
if (projectPath) {
if (turboSemVer && !semver.intersects(turboSemVer, '<1.8.0')) {
buildCommand = `turbo run build`;
} else {
// We don't know for sure if the local `turbo` supports inference.
buildCommand = `cd ${relativeToRoot} && turbo run build --filter={${projectPath}}...`;
}
}
return { return {
monorepoManager: 'turbo', monorepoManager: 'turbo',
buildCommand: projectPath buildCommand,
? `cd ${relativeToRoot} && npx turbo run build --filter={${projectPath}}...`
: null,
installCommand: installCommand:
packageManager === 'npm' packageManager === 'npm'
? `${packageManager} install --prefix=${relativeToRoot}` ? `${packageManager} install --prefix=${relativeToRoot}`

View File

@@ -0,0 +1,9 @@
{
"private": true,
"workspaces": [
"packages/*"
],
"devDependencies": {
"turbo": "1.8.0"
}
}

View File

@@ -0,0 +1,5 @@
{
"name": "app-14",
"version": "0.0.1",
"main": "index.js"
}

View File

@@ -0,0 +1,2 @@
// TEST COMMENT TO VERIFY JSON5 SUPPORT
{ "pipeline": { "build": { "dependsOn": ["^build"], "outputs": ["dist/**"] } } }

View File

@@ -26,24 +26,33 @@ describe('getMonorepoDefaultSettings', () => {
}); });
test.each([ test.each([
['turbo', 'turbo', false, 'app-14', false], ['turbo', 'turbo', false, 'app-14', false, false],
['turbo-package-config', 'turbo', false, 'app-13', false], ['turbo-has-filter', 'turbo', false, 'app-14', false, true],
['turbo-npm', 'turbo', true, 'app-15', false], ['turbo-package-config', 'turbo', false, 'app-13', false, false],
['turbo-npm-root-proj', 'turbo', true, 'app-root-proj', true], ['turbo-npm', 'turbo', true, 'app-15', false, false],
['nx', 'nx', false, 'app-12', false], ['turbo-npm-root-proj', 'turbo', true, 'app-root-proj', true, false],
['nx-package-config', 'nx', false, 'app-11', false], ['nx', 'nx', false, 'app-12', false, false],
['nx-project-and-package-config-1', 'nx', false, 'app-10', false], ['nx-package-config', 'nx', false, 'app-11', false, false],
['nx-project-and-package-config-2', 'nx', false, 'app-9', false], ['nx-project-and-package-config-1', 'nx', false, 'app-10', false, false],
['nx-project-config', 'nx', false, 'app-8', false], ['nx-project-and-package-config-2', 'nx', false, 'app-9', false, false],
['nx-project-config', 'nx', false, 'app-8', false, false],
])( ])(
'fixture %s', 'fixture %s',
async (fixture, expectedResultKey, isNpm, packageName, isRoot) => { async (
fixture,
expectedResultKey,
isNpm,
packageName,
isRoot,
supportsInference
) => {
const expectedResultMap: Record<string, Record<string, string>> = { const expectedResultMap: Record<string, Record<string, string>> = {
turbo: { turbo: {
monorepoManager: 'turbo', monorepoManager: 'turbo',
buildCommand: isRoot buildCommand:
? 'npx turbo run build' isRoot || supportsInference
: 'cd ../.. && npx turbo run build --filter={packages/app-1}...', ? 'turbo run build'
: 'cd ../.. && turbo run build --filter={packages/app-1}...',
installCommand: installCommand:
isNpm && isRoot isNpm && isRoot
? 'npm install' ? 'npm install'