summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Sima <ben@bensima.com>2025-11-26 22:45:16 -0500
committerBen Sima <ben@bensima.com>2025-11-26 22:50:01 -0500
commit63c795ba496d36d8551fef435804dba895ecb1cb (patch)
tree1f506afd8b979dffda1456e562f4dfa6439da3b0
parent37aa943173e2b15fabf22075ff56f8c9308c67ad (diff)
Bild: collect transitive langdeps for Python builds
detectPythonImports now returns (srcs, transitiveDeps) where transitiveDeps contains langdeps from all transitively imported local modules. This fixes builds where a module imports another local module that has language-level dependencies (e.g., pytest).
-rw-r--r--Omni/Bild.hs30
1 files changed, 16 insertions, 14 deletions
diff --git a/Omni/Bild.hs b/Omni/Bild.hs
index 335bc0f..3d14616 100644
--- a/Omni/Bild.hs
+++ b/Omni/Bild.hs
@@ -592,7 +592,7 @@ analyzeOne namespace@(Namespace parts ext) = do
contentLines
|> Meta.detectAll "#"
|> \Meta.Parsed {..} ->
- detectPythonImports mempty contentLines +> \srcs ->
+ detectPythonImports mempty contentLines +> \(srcs, transitiveDeps) ->
Target
{ builder = "python",
wrapper = Nothing,
@@ -609,7 +609,7 @@ analyzeOne namespace@(Namespace parts ext) = do
<> "', doraise=True)\""
],
sysdeps = psys,
- langdeps = pdep,
+ langdeps = pdep <> transitiveDeps,
outPath = outToPath pout,
out = pout <|> defaultOut,
packageSet = "python.packages",
@@ -875,14 +875,14 @@ detectLispImports contentLines =
|> Set.fromList
|> pure
--- | Finds local imports and recursively finds transitive imports, similar to
--- 'detectHaskellImports'.
-detectPythonImports :: Analysis -> [Text] -> IO (Set FilePath)
+-- | Finds local imports and recursively finds transitive imports and langdeps.
+-- Returns (srcs, transitive langdeps).
+detectPythonImports :: Analysis -> [Text] -> IO (Set FilePath, Set Meta.Dep)
detectPythonImports _ contentLines = do
root <- getCoderoot
let initialMods = catMaybes (Regex.match pythonImport </ (Text.unpack </ contentLines))
initialLocals <- toLocalFiles root initialMods
- bfs root (Set.fromList initialLocals) Set.empty
+ bfs root (Set.fromList initialLocals) Set.empty Set.empty
where
-- only detects 'import x' because I don't like 'from'
pythonImport :: Regex.RE Char String
@@ -892,9 +892,9 @@ detectPythonImports _ contentLines = do
*> Regex.many (Regex.psym isModuleChar)
<* Regex.many Regex.anySym
- bfs :: FilePath -> Set FilePath -> Set FilePath -> IO (Set FilePath)
- bfs root queue visited
- | Set.null queue = pure visited
+ bfs :: FilePath -> Set FilePath -> Set FilePath -> Set Meta.Dep -> IO (Set FilePath, Set Meta.Dep)
+ bfs root queue visited deps
+ | Set.null queue = pure (visited, deps)
| otherwise = do
let (rel, queue') = Set.deleteFindMin queue
fileLines <-
@@ -906,7 +906,9 @@ detectPythonImports _ contentLines = do
locals <- toLocalFiles root mods
let localsSet = Set.fromList locals
let newLocals = localsSet Set.\\ visited
- bfs root (queue' <> newLocals) (Set.insert rel visited)
+ -- Collect langdeps from this file's metadata
+ let Meta.Parsed {pdep = fileDeps} = Meta.detectAll "#" fileLines
+ bfs root (queue' <> newLocals) (Set.insert rel visited) (deps <> fileDeps)
toLocalFiles :: FilePath -> [String] -> IO [FilePath]
toLocalFiles root mods = do
@@ -918,11 +920,11 @@ test_detectPythonImports =
Test.group
"detectPythonImports"
[ Test.unit "matches import statements" <| do
- set <- detectPythonImports mempty ["import Omni.Log"]
- Set.fromList ["Omni/Log.py"] @=? set,
+ (srcs, _) <- detectPythonImports mempty ["import Omni.Log"]
+ Set.fromList ["Omni/Log.py"] @=? srcs,
Test.unit "matches import as statements" <| do
- set <- detectPythonImports mempty ["import Omni.Log as Log"]
- Set.fromList ["Omni/Log.py"] @=? set
+ (srcs, _) <- detectPythonImports mempty ["import Omni.Log as Log"]
+ Set.fromList ["Omni/Log.py"] @=? srcs
]
test_buildHsModuleGraph :: Test.Tree