From da0d78bf5b5c2d228e6868bad5dc0dc7ca7717dd Mon Sep 17 00:00:00 2001 From: Kazunori Kimura Date: Mon, 2 Oct 2023 18:34:45 +0900 Subject: [PATCH] Add build patch to avoid removing node_modules note: Only affected if using the relative_url feature `gitlab/script/frontent/preinstall.mjs` is introduced since v16.4.0. This script is executed on container startup, if using the relative url. This script removes `NODE_MODULES` (/home/git/gitlab/node_modules) when "the folder seems to end up being a corrupted somehow" See more detail: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/130938 On sameersbn/gitlab, the folder node_modules is declared as volume. The volume is always busy so that cannot be removed in the container. You can see following error reported on container startup (sameersbn/gitlab:16.4.0 or later). ```` yarn install v1.22.19 $ node ./scripts/frontend/preinstall.mjs [WARNING] package.json changed significantly. Removing node_modules to be sure there are no problems. node:internal/process/esm_loader:97 internalBinding('errors').triggerUncaughtException( ^ [Error: EBUSY: resource busy or locked, rmdir '/home/git/gitlab/node_modules'] { errno: -16, code: 'EBUSY', syscall: 'rmdir', path: '/home/git/gitlab/node_modules' } Node.js v18.17.1 error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command. ----- ```` This PR add a build time patch to change the behavior of script/frontend/preinstall.mjs to not to remove node_modules directly, but empty it instead. --- ...s-to-avoid-removing-node_modules_dir.patch | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 assets/build/patches/0003-fix_preinstall.mjs-to-avoid-removing-node_modules_dir.patch diff --git a/assets/build/patches/0003-fix_preinstall.mjs-to-avoid-removing-node_modules_dir.patch b/assets/build/patches/0003-fix_preinstall.mjs-to-avoid-removing-node_modules_dir.patch new file mode 100644 index 00000000..a5646ff3 --- /dev/null +++ b/assets/build/patches/0003-fix_preinstall.mjs-to-avoid-removing-node_modules_dir.patch @@ -0,0 +1,28 @@ +diff --git a/scripts/frontend/preinstall.mjs b/scripts/frontend/preinstall.mjs +index 09d980344eac..b1514e803b75 100644 +--- a/scripts/frontend/preinstall.mjs ++++ b/scripts/frontend/preinstall.mjs +@@ -1,6 +1,6 @@ + import { dirname, join } from 'node:path'; + import { fileURLToPath } from 'node:url'; +-import { readFile, rm } from 'node:fs/promises'; ++import { readdir, readFile, rm, stat } from 'node:fs/promises'; + + const ROOT_PATH = join(dirname(fileURLToPath(import.meta.url)), '..', '..'); + const NODE_MODULES = join(ROOT_PATH, 'node_modules'); +@@ -55,5 +55,14 @@ if (!arraysHaveSameItems(prevTopLevelPatterns, currentTopLevelPatterns)) { + console.error( + '[WARNING] package.json changed significantly. Removing node_modules to be sure there are no problems.', + ); +- await rm(NODE_MODULES, { recursive: true, force: true }); ++ // sameersbn/gitlab : avoid removing NODE_MODULES directly, iterate its content instead ++ // The path NODE_MODULES is declared as docker volume - always busy so that cannot be removed ++ // before iterating, check if the directory exists ++ const isDirectory = await stat(NODE_MODULES).then((stat) => stat.isDirectory()).catch(() => false); ++ if(isDirectory) { ++ for (const dir_ent of await readdir(NODE_MODULES, { withFileTypes: true})) { ++ const to_remove = join(NODE_MODULES, dir_ent.name); ++ await rm(to_remove, { recursive: true, force: true }); ++ } ++ } + }