Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-install lmdb when detecting the absence of @LMDB prebuilt packages #38691

Merged
merged 14 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,13 @@ jobs:
test_path: integration-tests/esm-in-gatsby-files
test_command: yarn test

integration_tests_lmdb_regeneration:
executor: node
steps:
- e2e-test:
test_path: integration-tests/lmdb-regeneration
test_command: yarn test

e2e_tests_path-prefix:
<<: *e2e-executor
steps:
Expand Down Expand Up @@ -590,6 +597,8 @@ workflows:
<<: *e2e-test-workflow
- integration_tests_esm_in_gatsby_files:
<<: *e2e-test-workflow
- integration_tests_lmdb_regeneration:
<<: *e2e-test-workflow
- integration_tests_gatsby_cli:
requires:
- bootstrap
Expand Down
3 changes: 3 additions & 0 deletions integration-tests/lmdb-regeneration/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__tests__/__debug__
node_modules
yarn.lock
1 change: 1 addition & 0 deletions integration-tests/lmdb-regeneration/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build_from_source=true
21 changes: 21 additions & 0 deletions integration-tests/lmdb-regeneration/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 gatsbyjs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions integration-tests/lmdb-regeneration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Artifacts test suite

This integration test suite helps us assert our mechanism to heal back from a broken lmdb installation is working by validating we install and use the correct prebundled binary for our platform
59 changes: 59 additions & 0 deletions integration-tests/lmdb-regeneration/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const path = require(`path`)
const execa = require(`execa`)
const mod = require("module")
const fs = require(`fs-extra`)

jest.setTimeout(100000)

const rootPath = path.resolve(__dirname, "../")

describe(`Lmdb regeneration`, () => {
test(`gatbsy build detects lmdb setup built from source and installs pre-buit package`, async () => {
const lmdbNodeModulesPath = path.resolve(rootPath, "node_modules", "lmdb")
// Make sure we clear out the current `@lmdb` optional dependencies
const pathsToRemove = [
path.resolve(rootPath, "node_modules", "@lmdb"),
path.resolve(rootPath, "node_modules", "gatsby", "node_modules", "@lmdb"),
]
for (let path of pathsToRemove) {
fs.rmSync(path, { force: true, recursive: true })
}
// Check the lmdb instance we have installed does have a binary built from source since we need it to reproduce the fix we're trying to test
// If this check fails then it means our fixture is wrong and we're relying on an lmdb instance with prebuilt binaries
const builtFromSource = fs.existsSync(
path.resolve(lmdbNodeModulesPath, "build", "Release", "lmdb.node")
)
expect(builtFromSource).toEqual(true)

const options = {
stderr: `inherit`,
stdout: `inherit`,
cwd: rootPath,
}
const gatsbyBin = path.resolve(rootPath, `node_modules`, `gatsby`, `cli.js`)
await execa(gatsbyBin, [`build`], options)

// lmdb module with prebuilt binaries for our platform
const lmdbPackage = `@lmdb/lmdb-${process.platform}-${process.arch}`

// If the fix worked correctly we should have installed the prebuilt binary for our platform under our `.cache` directory
const lmdbRequire = mod.createRequire(
path.resolve(rootPath, ".cache", "internal-packages", "package.json")
)
expect(() => {
lmdbRequire.resolve(lmdbPackage)
}).not.toThrow()

// The resulting query-engine bundle should not contain the binary built from source
const binaryBuiltFromSource = path.resolve(
rootPath,
".cache",
"query-engine",
"assets",
"build",
"Release",
"lmdb.node"
)
expect(fs.existsSync(binaryBuiltFromSource)).toEqual(false)
})
})
9 changes: 9 additions & 0 deletions integration-tests/lmdb-regeneration/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
siteMetadata: {
siteUrl: `https://www.yourdomain.tld`,
},
plugins: [],
flags: {
DEV_SSR: true,
},
}
3 changes: 3 additions & 0 deletions integration-tests/lmdb-regeneration/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
testPathIgnorePatterns: [`/node_modules/`, `__tests__/fixtures`, `.cache`],
}
22 changes: 22 additions & 0 deletions integration-tests/lmdb-regeneration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "lmdb-regeneration",
"private": true,
"author": "Sid Chatterjee",
"description": "A simplified bare-bones starter for Gatsby with DSG",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"build": "gatsby build",
"clean": "gatsby clean",
"test": "jest --runInBand"
},
"dependencies": {
"gatsby": "next",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"fs-extra": "^11.1.0",
"jest": "^29.3.1"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions integration-tests/lmdb-regeneration/src/pages/404.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as React from "react"
import { Link } from "gatsby"

const pageStyles = {
color: "#232129",
padding: "96px",
fontFamily: "-apple-system, Roboto, sans-serif, serif",
}
const headingStyles = {
marginTop: 0,
marginBottom: 64,
maxWidth: 320,
}

const paragraphStyles = {
marginBottom: 48,
}
const codeStyles = {
color: "#8A6534",
padding: 4,
backgroundColor: "#FFF4DB",
fontSize: "1.25rem",
borderRadius: 4,
}

const NotFoundPage = () => {
return (
<main style={pageStyles}>
<h1 style={headingStyles}>Page not found</h1>
<p style={paragraphStyles}>
Sorry 😔, we couldn’t find what you were looking for.
<br />
{process.env.NODE_ENV === "development" ? (
<>
<br />
Try creating a page in <code style={codeStyles}>src/pages/</code>.
<br />
</>
) : null}
<br />
<Link to="/">Go home</Link>.
</p>
</main>
)
}

export default NotFoundPage

export const Head = () => <title>Not found</title>
17 changes: 17 additions & 0 deletions integration-tests/lmdb-regeneration/src/pages/test-dsg.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from "react";

const DSG = () => {
return <h1>DSG</h1>;
};

export default DSG;

export const Head = () => <title>DSG</title>;

export async function config() {
return () => {
return {
defer: true,
};
};
}
106 changes: 103 additions & 3 deletions packages/gatsby/src/schema/graphql-engine/bundle-webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import * as path from "path"
import * as fs from "fs-extra"
import execa, { Options as ExecaOptions } from "execa"
import webpack, { Module, NormalModule, Compilation } from "webpack"
import ConcatenatedModule from "webpack/lib/optimize/ConcatenatedModule"
import { dependencies } from "gatsby/package.json"
import { printQueryEnginePlugins } from "./print-plugins"
import mod from "module"
import { WebpackLoggingPlugin } from "../../utils/webpack/plugins/webpack-logging"
Expand All @@ -12,6 +14,7 @@ import { schemaCustomizationAPIs } from "./print-plugins"
import type { GatsbyNodeAPI } from "../../redux/types"
import * as nodeApis from "../../utils/api-node-docs"
import { store } from "../../redux"
import { PackageJson } from "../../.."

type Reporter = typeof reporter

Expand All @@ -35,6 +38,92 @@ function getApisToRemoveForQueryEngine(): Array<GatsbyNodeAPI> {
return apisToRemove
}

const getInternalPackagesCacheDir = (): string =>
path.join(process.cwd(), `.cache/internal-packages`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if there is something we could do to "sandbox" this to not have potential repo/site level .npmrc (or even user level .npmrc locally) potentially cause problems with installing optional packages. If there is .npmrc with omit=optional (or however the syntax it is) - that would use in this directory as well?

Alternatively, maybe instead of installing whole lmdb (and let that install optional packages with prebuilds) - maybe we try to install those prebuild package directly? Maybe then it would be less prone to inherit same problem that cause those not to be installed normally in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, maybe instead of installing whole lmdb (and let that install optional packages with prebuilds) - maybe we try to install those prebuild package directly?

I guess what I wonder is, in the presence of @lmdb/x package will that always be favoured even if there's a build from source in the lmdb directory? (I can dig through the code to try to understand that but tbh I worry that we might miss something by doing so)

Copy link
Contributor

@pieh pieh Nov 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess what I wonder is, in the presence of @lmdb/x package will that always be favoured even if there's a build from source in the lmdb directory? (I can dig through the code to try to understand that but tbh I worry that we might miss something by doing so)

It looks like general order of loading is to first try to load from build/Release first before trying optional packages. However this relies on fact that there is install script that before trying to compile from source check if there is binary that can be loaded - and when prebuilts are installed - it finds those and that check prevent from even compiling from source ( https://unpkg.com/browse/node-gyp-build-optional-packages/ is doing all those things ).

However - our lmdb-bundling-patch replace runtime usage of node-gyp-build(-optional-packages) from lmdb and replace that with direct path to .node file, so we are not subject to regular behavior when bundling for lambdas as we get rid of it here:

.replace(
`require$1('node-gyp-build-optional-packages')(dirName)`,
`require(${JSON.stringify(lmdbBinaryLocation)})`
)
.replace(
`require$1('node-gyp-build')(dirName)`,
`require(${JSON.stringify(lmdbBinaryLocation)})`
)
.replace(
`loadNAPI__default["default"](dirName);`,
`require(${JSON.stringify(lmdbBinaryLocation)})`
)

So I think if we can install just one of prebuild packages like @lmdb/lmdb-linux-x64 and get path to .node file and make the lmdb bundling patch use that - I think this should be fine (?)

Also when we install whole lmdb in .cache/internal-packages, we don't actually use lmdb instance from there currently - we could add webpack alias to use that instance of lmdb package for lambda, but my biggest hang up with it is that installing main lmdb package it in nested directory could inherit any configuration that caused those prebuilt packages to not be installed in first place.

PS. after going through node-gyp-build-optional-packages I also found that:

build-from-source = true

in .npmrc would also likely cause the issue (similarly to omit = optional)


// Create a directory and JS module where we install internally used packages
const createInternalPackagesCacheDir = async (): Promise<void> => {
const cacheDir = getInternalPackagesCacheDir()
await fs.ensureDir(cacheDir)
await fs.emptyDir(cacheDir)

const packageJsonPath = path.join(cacheDir, `package.json`)

await fs.outputJson(packageJsonPath, {
name: `gatsby-internal-packages`,
description: `This directory contains internal packages installed by Gatsby used to comply with the current platform requirements`,
version: `1.0.0`,
private: true,
author: `Gatsby`,
license: `MIT`,
})
}

// lmdb module with prebuilt binaries for our platform
const lmdbPackage = `@lmdb/lmdb-${process.platform}-${process.arch}`

// Detect if the prebuilt binaries for lmdb have been installed. These are installed under @lmdb and are tied to each platform/arch. We've seen instances where regular installations lack these modules because of a broken lockfile or skipping optional dependencies installs
function installPrebuiltLmdb(): boolean {
// Read lmdb's package.json, go through its optional depedencies and validate if there's a prebuilt lmdb module with a compatible binary to our platform and arch
let packageJson: PackageJson
try {
const modulePath = path
.dirname(require.resolve(`lmdb`))
.replace(`/dist`, ``)
const packageJsonPath = path.join(modulePath, `package.json`)
packageJson = JSON.parse(fs.readFileSync(packageJsonPath, `utf-8`))
} catch (e) {
// If we fail to read lmdb's package.json there's bigger problems here so just skip installation
return false
}
// If there's no lmdb prebuilt package for our arch/platform listed as optional dep no point in trying to install it
const { optionalDependencies } = packageJson
if (!optionalDependencies) return false
if (!Object.keys(optionalDependencies).find(p => p === lmdbPackage))
return false
try {
const lmdbRequire = mod.createRequire(require.resolve(`lmdb`))
lmdbRequire.resolve(lmdbPackage)
return false
} catch (e) {
return true
}
}

// Install lmdb's native system module under our internal cache if we detect the current installation
// isn't using the pre-build binaries
async function installIfMissingLmdb(): Promise<string | undefined> {
if (!installPrebuiltLmdb()) return undefined

await createInternalPackagesCacheDir()

const cacheDir = getInternalPackagesCacheDir()
const options: ExecaOptions = {
stderr: `inherit`,
cwd: cacheDir,
}

const npmAdditionalCliArgs = [
`--no-progress`,
`--no-audit`,
`--no-fund`,
`--loglevel`,
`error`,
`--color`,
`always`,
`--legacy-peer-deps`,
`--save-exact`,
]

await execa(
`npm`,
[`install`, ...npmAdditionalCliArgs, `${lmdbPackage}@${dependencies.lmdb}`],
options
)

return path.join(cacheDir, `node_modules`, lmdbPackage)
}

export async function createGraphqlEngineBundle(
rootDir: string,
reporter: Reporter,
Expand All @@ -57,6 +146,19 @@ export async function createGraphqlEngineBundle(
require.resolve(`gatsby-plugin-typescript`)
)

// Alternative lmdb path we've created to self heal from a "broken" lmdb installation
const alternativeLmdbPath = await installIfMissingLmdb()

// We force a specific lmdb binary module if we detected a broken lmdb installation or if we detect the presence of an adapter
let forcedLmdbBinaryModule: string | undefined = undefined
if (store.getState().adapter.instance) {
forcedLmdbBinaryModule = `${lmdbPackage}/node.abi83.glibc.node`
}
// We always force the binary if we've installed an alternative path
if (alternativeLmdbPath) {
forcedLmdbBinaryModule = `${alternativeLmdbPath}/node.abi83.glibc.node`
}

const compiler = webpack({
name: `Query Engine`,
// mode: `production`,
Expand Down Expand Up @@ -121,9 +223,7 @@ export async function createGraphqlEngineBundle(
{
loader: require.resolve(`./lmdb-bundling-patch`),
options: {
forcedBinaryModule: store.getState().adapter.instance
? `@lmdb/lmdb-${process.platform}-${process.arch}/node.abi83.glibc.node`
: undefined,
forcedBinaryModule: forcedLmdbBinaryModule,
},
},
],
Expand Down