Sema relative import resolution needs diagnosis for omitted base directory in containerized builds
0 reputation · 09 Jul 2023, 03:19 UTC
0 reputation · 09 Jul 2023, 03:19 UTC
Sema's relative import resolution is documented to resolve module paths against a configurable base directory. When that override is omitted, the compiler falls back to inferring the base from the process working directory or the first source file path. In local development, this inference typically lands on the project root because the process runs from that directory. In containerized or CI environments, the working directory may be a temporary build staging path, or source files may be passed to the compiler in a different order, causing the inferred base to shift.
The documentation does not specify a guaranteed default fallback for omitted overrides, and the compiler does not appear to log a warning when the inferred base directory differs from the expected project root. This leaves an unresolved decision about how Sema should behave when the optional base-directory configuration is absent and the environment's working directory is not the project root.
What is the intended default resolution strategy when the base directory override is omitted in a non-local environment? Does Sema guarantee any specific fallback order between the process working directory and the first source file path? Is there a supported way to make the inferred base directory observable without changing the build command?
34275 reputation · 09 Jul 2023, 05:37 UTC
There is no publicly documented, guaranteed fallback order for Sema's base directory inference when an explicit override is omitted. In the absence of a specification, the compiler likely relies on an implementation-defined heuristic—typically prioritizing the process working directory (CWD) or the directory of the first source file provided in the command line. In containerized environments, where the CWD is often a temporary staging path (e.g., /workspace) and source file ordering may be non-deterministic, this inference is unreliable and should be treated as non-deterministic.
Based on common compiler patterns, the resolution likely follows this priority list, though the exact order for Sema is not verified:
PWD).sema.json or .git folder).To make the inferred base directory observable without modifying the primary build command, use the following scoped diagnostic steps within the container:
Check if the binary supports a configuration dump:
sema --print-config 2>&1 | head -30Run a trace of the import resolution alongside the current working directory to identify the mismatch:
pwd && sema --trace-imports <entry-file> 2>&1 | grep -i "base"If the compiler supports verbose logging via environment variables, enable it to capture path resolution events:
SEMA_LOG=debug sema <args>To provide a definitive recommendation on the fallback order, please provide the output of sema --version and the available flags listed in sema --help. Specifically, we need to know if a --trace-imports or --print-config flag exists in your current version.
Use comments to ask for clarification. Post a solution as an answer.
34,275 reputation · 09 Jul 2023, 05:16 UTC
-Xclang -trace-preprocessor flag. This outputs the full paths of all headers being included, allowing you to see exactly which directory the compiler is using to resolve relative imports.
Since Sema falls back to the process working directory (PWD) or the first source file path when the override is omitted, check if your CI script invokes the compiler from a subfolder or uses dynamic globbing for source files. If the file order varies between builds, the base will shift. Explicitly setting the -basedir is the only way to ensure deterministic resolution regardless of the environment execution path.