Skip to content

Commit

Permalink
Warn on no descriptor match rather than fail analysis.
Browse files Browse the repository at this point in the history
  • Loading branch information
csasarak committed May 31, 2024
1 parent 7a3162f commit c8ea269
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/Strategy/Node/PackageJson.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import Data.Glob (Glob)
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Set (Set)
import Data.String.Conversion (ToText)
import Data.String.Conversion (ToText (toText))
import Data.Tagged (Tagged)
import Data.Text (Text)
import DepTypes (
Expand Down Expand Up @@ -232,3 +232,7 @@ data NodePackage = NodePackage
, pkgConstraint :: Text
}
deriving (Eq, Ord, Show)

instance ToText NodePackage where
toText NodePackage{pkgName, pkgConstraint} =
pkgName <> "@" <> pkgConstraint
2 changes: 1 addition & 1 deletion src/Strategy/Node/YarnV2/Resolvers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ data Package

-- | Search for a resolver that supports the Locator, and turn it into a Package
resolveLocatorToPackage :: Has Diagnostics sig m => Locator -> m Package
resolveLocatorToPackage locator = context ("Resolving locator " <> showT locator) $ do
resolveLocatorToPackage locator = context ("Resolving " <> showT locator) $ do
resolver <-
fromMaybe @Text "Unsupported locator (no resolver found)" $
find (`resolverSupportsLocator` locator) allResolvers
Expand Down
36 changes: 23 additions & 13 deletions src/Strategy/Node/YarnV2/YarnLock.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import Control.Effect.Diagnostics (
Has,
ToDiagnostic (renderDiagnostic),
context,
warn,
warnLeft,
)
import Control.Effect.Diagnostics qualified as Diag (fromMaybe)
import Data.Either.Combinators (maybeToRight)
import Data.Foldable (find)
import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
Expand Down Expand Up @@ -89,20 +90,25 @@ analyze file flatdeps = context "Lockfile V2 analysis" $ do
resolveFlatDeps :: Has Diagnostics sig m => YarnLockfile -> FlatDeps -> m FlatPackages
resolveFlatDeps lockfile FlatDeps{..} = FlatPackages <$> converter @Production directDeps <*> converter @Development devDeps
where
resolvePackages :: Has Diagnostics sig m => [NodePackage] -> m [Package]
resolvePackages = fmap catMaybes . traverse (resolveSingle lockfile)

converter ::
forall tag sig m.
Has Diagnostics sig m =>
Tagged tag (Set NodePackage) ->
m (Tagged tag (Set Package))
converter = fmap (applyTag . Set.fromList) . traverse (resolveSingle lockfile) . Set.toList . unTag
converter = fmap (applyTag . Set.fromList) . resolvePackages . Set.toList . unTag

resolveSingle :: Has Diagnostics sig m => YarnLockfile -> NodePackage -> m Package
resolveSingle (YarnLockfile lockfileMap) nodePkg = do
descriptor <-
Diag.fromMaybe (DescriptorParse nodePkg) $
tryParseDescriptor nodePkg
loc <- lookupPackage descriptor $ remap lockfileMap
resolveLocatorToPackage $ descResolution loc
resolveSingle :: Has Diagnostics sig m => YarnLockfile -> NodePackage -> m (Maybe Package)
resolveSingle (YarnLockfile lockfileMap) nodePkg =
do
descriptor <-
Diag.fromMaybe (DescriptorParse nodePkg) $
tryParseDescriptor nodePkg
let lookupRes = lookupPackage descriptor $ remap lockfileMap
res <- context ("Resolve " <> toText nodePkg) $ warnLeft lookupRes
traverse (resolveLocatorToPackage . descResolution) res

remap :: Ord k => Map [k] a -> Map k a
remap = Map.fromList . concatMap (\(ks, v) -> map (,v) ks) . Map.toList
Expand All @@ -111,15 +117,19 @@ remap = Map.fromList . concatMap (\(ks, v) -> map (,v) ks) . Map.toList
--
-- This ensures that all dependency relationships are valid
stitchLockfile :: Has Diagnostics sig m => YarnLockfile -> m (AM.AdjacencyMap Locator)
stitchLockfile (YarnLockfile lockfile) = graph
stitchLockfile (YarnLockfile lockfile) = context ("Stitching Lockfile") $
do graph
where
-- remapping @Map [Descriptor] PackageDescription@ to @Map Descriptor PackageDescription@
remapped :: Map Descriptor PackageDescription
remapped = remap lockfile

lookupPackage' :: Has Diagnostics sig m => Descriptor -> m (Maybe PackageDescription)
lookupPackage' pkg = warnLeft $ lookupPackage pkg remapped

-- look up all of a package's dependencies as locators in the lockfile
lookupPackageDeps :: Has Diagnostics sig m => PackageDescription -> m [Locator]
lookupPackageDeps = fmap (map descResolution) . traverse (`lookupPackage` remapped) . descDependencies
lookupPackageDeps = fmap (map descResolution . catMaybes) . traverse lookupPackage' . descDependencies

-- build the edges (adjacency list) between a package and its dependencies
packageToEdges :: Has Diagnostics sig m => PackageDescription -> m [(Locator, Locator)]
Expand Down Expand Up @@ -149,9 +159,9 @@ stitchLockfile (YarnLockfile lockfile) = graph
-- npm dependencies. For example, given dependencies on @package: ^1.0.0@
-- and @package: ^2.0.0@, only @package@npm:^2.0.0@ will appear as a
-- descriptor key for a package in the lockfile
lookupPackage :: Has Diagnostics sig m => Descriptor -> Map Descriptor PackageDescription -> m PackageDescription
lookupPackage :: Descriptor -> Map Descriptor PackageDescription -> Either NoPackageForDescriptor PackageDescription
lookupPackage desc mapping =
Diag.fromMaybe (NoPackageForDescriptor desc) $
maybeToRight (NoPackageForDescriptor desc) $
Map.lookup desc mapping <|> Map.lookup (desc{descriptorRange = "npm:" <> descriptorRange desc}) mapping <|> lookupAnyNpm desc mapping
where
-- find any package with a descriptor with matching scope/name, and an @npm:@ prefix prefix
Expand Down

0 comments on commit c8ea269

Please sign in to comment.