nodejs/node · error · GypError
Could not automatically locate src directory. This isa temp
Error message
Could not automatically locate src directory. This isa temporary Chromium feature that will be removed. Use--depth as a workaround.
What it means
GypError('Could not automatically locate src directory...') is raised when --depth was not supplied and gyp's Chromium-specific heuristic fails: it walks each build file's absolute path components looking for a directory literally named 'src', and none is found. The 'depth' is used as the source-tree root for relative target paths. The message itself notes this is a legacy Chromium feature and recommends --depth.
Source
Thrown at tools/gyp/pylib/gyp/__init__.py:592
# found will be treated as though it were the path used for --depth.
if not options.depth:
for build_file in build_files:
build_file_dir = os.path.abspath(os.path.dirname(build_file))
build_file_dir_components = build_file_dir.split(os.path.sep)
components_len = len(build_file_dir_components)
for index in range(components_len - 1, -1, -1):
if build_file_dir_components[index] == "src":
options.depth = os.path.sep.join(build_file_dir_components)
break
del build_file_dir_components[index]
# If the inner loop found something, break without advancing to another
# build file.
if options.depth:
break
if not options.depth:
raise GypError(
"Could not automatically locate src directory. This is"
"a temporary Chromium feature that will be removed. Use"
"--depth as a workaround."
)
# If toplevel-dir is not set, we assume that depth is the root of our source
# tree.
if not options.toplevel_dir:
options.toplevel_dir = options.depth
# -D on the command line sets variable defaults - D isn't just for define,
# it's for default. Perhaps there should be a way to force (-F?) a
# variable's value so that it can't be overridden by anything else.
cmdline_default_variables = {}
defines = []
if options.use_environment:
defines += ShlexEnv("GYP_DEFINES")
if options.defines:View on GitHub (pinned to 1b2de5e052)
Solutions
- Pass --depth=<path-to-source-root> explicitly on the gyp command line.
- Ensure the build file lives under a directory chain containing a 'src' component, or rename your root to 'src'.
- Set options.depth in your wrapper script before calling gyp.main.
Example fix
# before python gyp project.gyp # after python gyp --depth=. project.gyp
Defensive patterns
Strategy: validation
Validate before calling
import os
def resolve_depth(build_file):
parts = os.path.abspath(os.path.dirname(build_file)).split(os.path.sep)
if 'src' in parts:
return os.path.sep.join(parts[:parts.index('src') + 1])
return os.getcwd() # explicit fallback instead of relying on heuristic
depth = resolve_depth(build_file) Type guard
def path_has_src_component(build_file: str) -> bool:
import os
return 'src' in os.path.abspath(build_file).split(os.path.sep) Try / catch
try:
gyp_main(args)
except GypError as e:
if 'src directory' in str(e):
args = ['--depth=' + os.getcwd()] + args # supply depth, retry
raise Prevention
- Always pass --depth explicitly; do not rely on the Chromium 'src' heuristic.
- For non-Chromium projects, --depth=. is usually correct.
- Name the source root 'src' if you want the heuristic to work.
When it happens
Trigger: Running gyp without --depth on a build file whose path contains no 'src' component; checking out only a subtree of a Chromium-style repo; renaming the top-level source directory away from 'src'.
Common situations: Non-Chromium projects that nonetheless use gyp and don't have a 'src' dir; shallow clones that omit the conventional layout; vendoring a gyp project under a differently named root.
Related errors
- %s: error: no build_file
- `${baseKey}` is not a valid npm option
- The `${baseKey}` option is deprecated, and cannot be set in
- The ${key} option is protected, and cannot be retrieved in t
- Tag name must not be a valid SemVer range: ${defaultTag.trim
AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13).
Data as JSON: /api/errors/7aac2a5915e8066a.
Report an issue: GitHub.