Repository: netlify/build (js)

Issue:
Bug: build-info framework detection wrong publish directory for gatsby
See this thread for more visibility https://netlify.slack.com/archives/C01TKAEBP3Q/p1688457004224069.

The build-info framework detection seems to have a bug on the publish directory where it provides the static assets directory instead of the publish one. The static assets is only meant for the inputs and not as a directory we are going to publish.

Pull request:
fix: fixes an issue where the wrong dist was used for frameworks with…
… staticAssetsDirectory

🎉 Thanks for submitting a pull request! 🎉

#### Summary

Fixes #5121 



---

For us to review and ship your PR efficiently, please perform the following steps:

- [ ] Open a [bug/issue](https://github.com/netlify/build/issues/new/choose) before writing your code 🧑‍💻. This ensures
      we can discuss the changes and get feedback from everyone that should be involved. If you\`re fixing a typo or
      something that\`s on fire 🔥 (e.g. incident related), you can skip this step.
- [ ] Read the [contribution guidelines](https://github.com/netlify/build/blob/main/CONTRIBUTING.md) 📖. This ensures
      your code follows our style guide and passes our tests.
- [ ] Update or add tests (if any source code was changed or added) 🧪
- [ ] Update or add documentation (if features were changed or added) 📝
- [ ] Make sure the status checks below are successful ✅

**A picture of a cute animal (not mandatory, but encouraged)**


Patch:
diff --git a/packages/build-info/src/settings/get-build-settings.ts b/packages/build-info/src/settings/get-build-settings.ts
index 05d24c235..dc5042f21 100644
--- a/packages/build-info/src/settings/get-build-settings.ts
+++ b/packages/build-info/src/settings/get-build-settings.ts
@@ -69,8 +69,6 @@ async function applyBuildSystemOverrides(
 }
 
 async function getSettings(framework: Framework, project: Project, baseDirectory: string): Promise<Settings> {
-  const frameworkDist = framework.staticAssetsDirectory || framework.build.directory
-
   const devCommands = framework.getDevCommands()
   const buildCommands = framework.getBuildCommands()
 
@@ -79,7 +77,7 @@ async function getSettings(framework: Framework, project: Project, baseDirectory
     buildCommand: buildCommands[0],
     devCommand: devCommands[0],
     frameworkPort: framework.dev?.port,
-    dist: project.fs.join(baseDirectory, frameworkDist),
+    dist: project.fs.join(baseDirectory, framework.build.directory),
     env: framework.env || {},
     plugins_from_config_file: [],
     plugins_recommended: framework.plugins || [],
@@ -93,7 +91,7 @@ async function getSettings(framework: Framework, project: Project, baseDirectory
   }
 
   if (baseDirectory?.length && project.workspace?.isRoot) {
-    settings.dist = project.fs.join(baseDirectory, frameworkDist)
+    settings.dist = project.fs.join(baseDirectory, framework.build.directory)
   }
 
   // 1. try to apply overrides for package managers (like npm, pnpm or yarn workspaces)


Test patch:
diff --git a/packages/build-info/src/build-systems/package-managers.test.ts b/packages/build-info/src/build-systems/package-managers.test.ts
index 38c93ab03..840df4856 100644
--- a/packages/build-info/src/build-systems/package-managers.test.ts
+++ b/packages/build-info/src/build-systems/package-managers.test.ts
@@ -35,7 +35,7 @@ describe('PNPM workspaces', () => {
         baseDirectory: '', // executed from root
         buildCommand: 'pnpm run build --filter astro-app',
         devCommand: 'pnpm run dev --filter astro-app',
-        dist: join('apps/astro/public'),
+        dist: join('apps/astro/dist'),
       }),
       expect.objectContaining({
         baseDirectory: '', // executed from root
@@ -72,7 +72,7 @@ describe('NPM workspaces', () => {
         baseDirectory: '', // executed from root
         buildCommand: 'npm run build --workspace astro-app',
         devCommand: 'npm run dev --workspace astro-app',
-        dist: join('apps/astro/public'),
+        dist: join('apps/astro/dist'),
       }),
       expect.objectContaining({
         baseDirectory: '', // executed from root
diff --git a/packages/build-info/src/frameworks/gatsby.test.ts b/packages/build-info/src/frameworks/gatsby.test.ts
index cc6029889..1dc6f92cb 100644
--- a/packages/build-info/src/frameworks/gatsby.test.ts
+++ b/packages/build-info/src/frameworks/gatsby.test.ts
@@ -13,6 +13,7 @@ test('should not add the plugin if the node version is below 12.13.0', async ({
     'package.json': JSON.stringify({ dependencies: { gatsby: '^4.0.0' } }),
     'gatsby-config.js': '',
   })
+  fs.cwd = cwd
   const detected = await new Project(fs, cwd).setNodeVersion('12.12.9').detectFrameworks()
   expect(detected?.[0].id).toBe('gatsby')
   expect(detected?.[0].plugins).toMatchObject([])
@@ -23,6 +24,7 @@ test('should detect a simple Gatsby project and add the plugin if the node versi
     'package.json': JSON.stringify({ dependencies: { gatsby: '^4.0.0' } }),
     'gatsby-config.js': '',
   })
+  fs.cwd = cwd
   const detected = await new Project(fs, cwd).setNodeVersion('12.13.0').detectFrameworks()
   expect(detected?.[0].id).toBe('gatsby')
   expect(detected?.[0].plugins).toMatchObject(['@netlify/plugin-gatsby'])
@@ -33,7 +35,9 @@ test('should detect a simple Gatsby 4 project', async ({ fs }) => {
     'package.json': JSON.stringify({ dependencies: { gatsby: '^4.0.0' } }),
     'gatsby-config.js': '',
   })
-  const detected = await new Project(fs, cwd).detectFrameworks()
+  fs.cwd = cwd
+  const project = new Project(fs, cwd)
+  const detected = await project.detectFrameworks()
   expect(detected?.[0].id).toBe('gatsby')
   expect(detected?.[0].build.command).toBe('gatsby build')
   expect(detected?.[0].build.directory).toBe('public')
@@ -43,6 +47,31 @@ test('should detect a simple Gatsby 4 project', async ({ fs }) => {
       NODE_VERSION: '14',
     }),
   )
+
+  expect(await project.getBuildSettings()).toEqual([
+    {
+      baseDirectory: '',
+      buildCommand: 'gatsby build',
+      devCommand: 'gatsby develop',
+      dist: 'public',
+      env: {
+        AWS_LAMBDA_JS_RUNTIME: 'nodejs14.x',
+        GATSBY_LOGGER: 'yurnalist',
+        GATSBY_PRECOMPILE_DEVELOP_FUNCTIONS: 'true',
+        NODE_VERSION: '14',
+      },
+      framework: {
+        id: 'gatsby',
+        name: 'Gatsby',
+      },
+      frameworkPort: 8000,
+      name: 'Gatsby',
+      packagePath: '',
+      plugins_from_config_file: [],
+      plugins_recommended: [],
+      pollingStrategies: ['TCP', 'HTTP'],
+    },
+  ])
 })
 
 test('should detect a simple Gatsby 5 project', async ({ fs }) => {
@@ -50,6 +79,7 @@ test('should detect a simple Gatsby 5 project', async ({ fs }) => {
     'package.json': JSON.stringify({ dependencies: { gatsby: '^5.0.0' } }),
     'gatsby-config.js': '',
   })
+  fs.cwd = cwd
   const detected = await new Project(fs, cwd).detectFrameworks()
   expect(detected?.[0].id).toBe('gatsby')
   expect(detected?.[0].env).toMatchObject(
diff --git a/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap b/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap
index 24ca5d570..31a8287a2 100644
--- a/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap
+++ b/packages/build-info/src/node/__snapshots__/get-build-info.test.ts.snap
@@ -54,7 +54,7 @@ exports[`should retrieve the build info for providing a rootDir 1`] = `
       "baseDirectory": "",
       "buildCommand": "pnpm run build --filter @evilcorp/blog",
       "devCommand": "pnpm run dev --filter @evilcorp/blog",
-      "dist": "packages/blog/public",
+      "dist": "packages/blog/dist",
       "env": {},
       "framework": {
         "id": "astro",
@@ -149,7 +149,7 @@ exports[`should retrieve the build info for providing a rootDir and a nested pro
       "baseDirectory": "",
       "buildCommand": "pnpm run build --filter @evilcorp/blog",
       "devCommand": "pnpm run dev --filter @evilcorp/blog",
-      "dist": "packages/blog/public",
+      "dist": "packages/blog/dist",
       "env": {},
       "framework": {
         "id": "astro",
@@ -223,7 +223,7 @@ exports[`should retrieve the build info for providing a rootDir and the same pro
       "baseDirectory": "",
       "buildCommand": "pnpm run build --filter @evilcorp/blog",
       "devCommand": "pnpm run dev --filter @evilcorp/blog",
-      "dist": "packages/blog/public",
+      "dist": "packages/blog/dist",
       "env": {},
       "framework": {
         "id": "astro",
diff --git a/packages/build-info/src/settings/get-build-settings.test.ts b/packages/build-info/src/settings/get-build-settings.test.ts
index c401bb818..e007e498f 100644
--- a/packages/build-info/src/settings/get-build-settings.test.ts
+++ b/packages/build-info/src/settings/get-build-settings.test.ts
@@ -125,7 +125,7 @@ test('get dev command from npm scripts if defined inside a workspace setup', asy
       baseDirectory: '', // executed via npm run so no base directory needed we can run from the root
       buildCommand: 'npm run site:build --workspace svelte-app',
       devCommand: 'npm run site:start --workspace svelte-app',
-      dist: join('apps/svelte/static'),
+      dist: join('apps/svelte/public'),
     }),
   ])
 })
@@ -172,7 +172,7 @@ describe.each([
           packagePath: platformJoin('packages/blog'),
           buildCommand: 'npm run build --workspace @evilcorp/blog',
           devCommand: 'npm run dev --workspace @evilcorp/blog',
-          dist: platformJoin('packages/blog/public'),
+          dist: platformJoin('packages/blog/dist'),
         }),
         expect.objectContaining({
           baseDirectory: '',
@@ -194,7 +194,7 @@ describe.each([
           baseDirectory: '',
           buildCommand: 'npm run build --workspace @evilcorp/blog',
           devCommand: 'npm run dev --workspace @evilcorp/blog',
-          dist: platformJoin('packages/blog/public'),
+          dist: platformJoin('packages/blog/dist'),
           packagePath: platformJoin('packages/blog'),
         }),
       ])
@@ -218,7 +218,7 @@ describe.each([
           packagePath: platformJoin('packages/blog'),
           buildCommand: 'pnpm run build --filter @evilcorp/blog',
           devCommand: 'pnpm run dev --filter @evilcorp/blog',
-          dist: platformJoin('packages/blog/public'),
+          dist: platformJoin('packages/blog/dist'),
         }),
         expect.objectContaining({
           baseDirectory: '',
@@ -240,7 +240,7 @@ describe.each([
           baseDirectory: '',
           buildCommand: 'pnpm run build --filter @evilcorp/blog',
           devCommand: 'pnpm run dev --filter @evilcorp/blog',
-          dist: platformJoin('packages/blog/public'),
+          dist: platformJoin('packages/blog/dist'),
           packagePath: platformJoin('packages/blog'),
         }),
       ])
@@ -264,7 +264,7 @@ describe.each([
           packagePath: platformJoin('packages/blog'),
           buildCommand: 'yarn workspace @evilcorp/blog build',
           devCommand: 'yarn workspace @evilcorp/blog dev',
-          dist: platformJoin('packages/blog/public'),
+          dist: platformJoin('packages/blog/dist'),
         }),
         expect.objectContaining({
           baseDirectory: '',
@@ -409,7 +409,7 @@ describe.each([
           packagePath: platformJoin('apps/svelte-app'),
           buildCommand: 'nx run svelte-app:build',
           devCommand: 'nx run svelte-app:dev',
-          dist: platformJoin('apps/svelte-app/static'),
+          dist: platformJoin('apps/svelte-app/build'),
         }),
         expect.objectContaining({
           baseDirectory: '',