| name | build-script-path-rot |
| description | Hardcoded paths in build scripts break when directory structure changes: |
| lastReviewed | 2026-04-30T00:00:00.000Z |
Build Script Path Rot
The Problem
Hardcoded paths in build scripts break when directory structure changes:
const srcDir = './src/components';
const outDir = './dist/components';
The Solution
Option 1: Resolve Relative to Manifest
const path = require('path');
const pkg = require('./package.json');
const srcDir = path.resolve(__dirname, pkg.directories?.src || 'src');
const outDir = path.resolve(__dirname, pkg.directories?.dist || 'dist');
{
"directories": {
"src": "packages/ui/src",
"dist": "dist"
}
}
Option 2: Config File
module.exports = {
srcDir: './packages/ui/src',
outDir: './dist',
assets: './assets'
};
const config = require('./build.config.js');
const srcDir = path.resolve(__dirname, config.srcDir);
Option 3: Auto-Discover
function findSrcDir() {
const candidates = ['src', 'packages/ui/src', 'lib'];
for (const dir of candidates) {
if (fs.existsSync(path.join(dir, 'index.ts'))) {
return dir;
}
}
throw new Error('Could not find source directory');
}
Red Flags
| Pattern | Risk |
|---|
'./src/' hardcoded | Will break on restructure |
process.cwd() + '/src' | Breaks if script run from different dir |
Relative paths without path.resolve | Platform-specific issues |
Safe Patterns
const scriptDir = __dirname;
const projectRoot = path.resolve(scriptDir, '..');
const srcDir = path.resolve(projectRoot, 'src');
const pkgPath = require.resolve('./package.json');
const projectRoot = path.dirname(pkgPath);
Verification
- Move a directory → script still works (reads from config)
- Run script from different working directory → still works
- Paths in config match actual structure
When to Apply
- Any build script with file paths
- After directory restructure
- When script "mysteriously" breaks in CI
Tags
build paths configuration portability