-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prune unused dependencies from manifests generated from spago.dhall files #667
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bf1f58d
Add purs graph module to lib
thomashoneyman 6b4e5c8
Add tests for purs graph lib module
thomashoneyman 1f4bb2d
Add purs graph CLI command and test
thomashoneyman 3e90b19
Implement unused dependency check in API
thomashoneyman 385340d
Extract and test install path parser function
thomashoneyman fc6db7f
Add prune check to the end-to-end test.
thomashoneyman 2c04da9
Set minimum threshold on purs graph compiler
thomashoneyman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"type-equality","version":"4.0.1","license":"BSD-3-Clause","location":{"githubOwner":"purescript","githubRepo":"purescript-type-equality"},"dependencies":{}} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"location": { | ||
"githubOwner": "purescript", | ||
"githubRepo": "purescript-type-equality" | ||
}, | ||
"published": { | ||
"4.0.1": { | ||
"bytes": 2184, | ||
"hash": "sha256-Hs9D6Y71zFi/b+qu5NSbuadUQXe5iv5iWx0226vOHUw=", | ||
"publishedTime": "2022-04-27T18:00:18.000Z", | ||
"ref": "v4.0.1" | ||
} | ||
}, | ||
"unpublished": {} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,15 @@ import Data.Array.NonEmpty as NonEmptyArray | |
import Data.Foldable (traverse_) | ||
import Data.Map as Map | ||
import Data.Set as Set | ||
import Data.String as String | ||
import Data.String.NonEmpty as NonEmptyString | ||
import Effect.Aff as Aff | ||
import Effect.Ref as Ref | ||
import Node.FS.Aff as FS.Aff | ||
import Node.Path as Path | ||
import Node.Process as Process | ||
import Registry.App.API as API | ||
import Registry.App.CLI.Tar as Tar | ||
import Registry.App.Effect.Env as Env | ||
import Registry.App.Effect.Log as Log | ||
import Registry.App.Effect.Pursuit as Pursuit | ||
|
@@ -23,7 +25,10 @@ import Registry.Constants as Constants | |
import Registry.Foreign.FSExtra as FS.Extra | ||
import Registry.Foreign.FastGlob as FastGlob | ||
import Registry.Foreign.Tmp as Tmp | ||
import Registry.Internal.Codec as Internal.Codec | ||
import Registry.Manifest as Manifest | ||
import Registry.PackageName as PackageName | ||
import Registry.Range as Range | ||
import Registry.Test.Assert as Assert | ||
import Registry.Test.Assert.Run as Assert.Run | ||
import Registry.Test.Utils as Utils | ||
|
@@ -52,11 +57,24 @@ spec = do | |
removeIgnoredTarballFiles | ||
copySourceFiles | ||
|
||
Spec.describe "Parses installed paths" do | ||
Spec.it "Parses install path <tmp>/my-package-1.0.0/..." do | ||
tmp <- Tmp.mkTmpDir | ||
let moduleA = Path.concat [ tmp, "my-package-1.0.0", "src", "ModuleA.purs" ] | ||
case API.parseInstalledModulePath { prefix: tmp, path: moduleA } of | ||
Left err -> Assert.fail $ "Expected to parse " <> moduleA <> " but got error: " <> err | ||
Right { name, version } -> do | ||
Assert.shouldEqual name (Utils.unsafePackageName "my-package") | ||
Assert.shouldEqual version (Utils.unsafeVersion "1.0.0") | ||
FS.Extra.remove tmp | ||
|
||
Spec.describe "API pipelines run correctly" $ Spec.around withCleanEnv do | ||
Spec.it "Publish" \{ workdir, index, metadata, storageDir, githubDir } -> do | ||
Spec.it "Publish a legacy-converted package with unused deps" \{ workdir, index, metadata, storageDir, githubDir } -> do | ||
let testEnv = { workdir, index, metadata, username: "jon", storage: storageDir, github: githubDir } | ||
Assert.Run.runTestEffects testEnv do | ||
-- We'll publish [email protected] | ||
-- We'll publish [email protected] from the fixtures/github-packages | ||
-- directory, which has an unnecessary dependency on 'type-equality' | ||
-- inserted into it. | ||
let | ||
name = Utils.unsafePackageName "effect" | ||
version = Utils.unsafeVersion "4.0.0" | ||
|
@@ -83,11 +101,28 @@ spec = do | |
unless (Set.member version versions) do | ||
Except.throw $ "Expected " <> formatPackageVersion name version <> " to be published to registry storage." | ||
|
||
-- Let's verify the manifest does not include the unnecessary | ||
-- 'type-equality' dependency... | ||
Storage.download name version "effect-result" | ||
Tar.extract { cwd: workdir, archive: "effect-result" } | ||
Run.liftAff (readJsonFile Manifest.codec (Path.concat [ "effect-4.0.0", "purs.json" ])) >>= case _ of | ||
Left err -> Except.throw $ "Expected [email protected] to be downloaded to effect-4.0.0 with a purs.json but received error " <> err | ||
Right (Manifest manifest) -> do | ||
let expectedDeps = Map.singleton (Utils.unsafePackageName "prelude") (Utils.unsafeRange ">=6.0.0 <7.0.0") | ||
when (manifest.dependencies /= expectedDeps) do | ||
Except.throw $ String.joinWith "\n" | ||
[ "Expected [email protected] to have dependencies" | ||
, printJson (Internal.Codec.packageMap Range.codec) expectedDeps | ||
, "\nbut got" | ||
, printJson (Internal.Codec.packageMap Range.codec) manifest.dependencies | ||
] | ||
|
||
-- Finally, we can verify that publishing the package again should fail | ||
-- since it already exists. | ||
Except.runExcept (API.publish CurrentPackage publishArgs) >>= case _ of | ||
Left _ -> pure unit | ||
Right _ -> Except.throw $ "Expected publishing " <> formatPackageVersion name version <> " twice to fail." | ||
|
||
where | ||
withCleanEnv :: (PipelineEnv -> Aff Unit) -> Aff Unit | ||
withCleanEnv action = do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
-- | A module describing the output of 'purs graph' along with some helper | ||
-- | functions for working with the graph. | ||
module Registry.PursGraph where | ||
|
||
import Prelude | ||
|
||
import Data.Array as Array | ||
import Data.Array.NonEmpty (NonEmptyArray) | ||
import Data.Array.NonEmpty as NonEmptyArray | ||
import Data.Bifunctor (bimap) | ||
import Data.Codec.Argonaut (JsonCodec) | ||
import Data.Codec.Argonaut as CA | ||
import Data.Codec.Argonaut.Record as CA.Record | ||
import Data.Either (Either(..)) | ||
import Data.Map (Map) | ||
import Data.Map as Map | ||
import Data.Maybe (Maybe(..)) | ||
import Data.Newtype (class Newtype, un) | ||
import Data.Profunctor as Profunctor | ||
import Data.Set (Set) | ||
import Data.Set as Set | ||
import Data.Traversable (traverse) | ||
import Data.Tuple (Tuple(..)) | ||
import Node.Path (FilePath) | ||
import Registry.Internal.Codec as Internal.Codec | ||
import Registry.PackageName (PackageName) | ||
|
||
-- | A graph of the dependencies between modules, discovered by the purs | ||
-- | compiler from a set of source files. | ||
type PursGraph = Map ModuleName PursGraphNode | ||
|
||
pursGraphCodec :: JsonCodec PursGraph | ||
pursGraphCodec = Internal.Codec.strMap "PursGraph" (Just <<< ModuleName) (un ModuleName) pursGraphNodeCodec | ||
|
||
type PursGraphNode = | ||
{ depends :: Array ModuleName | ||
, path :: FilePath | ||
} | ||
|
||
pursGraphNodeCodec :: JsonCodec PursGraphNode | ||
pursGraphNodeCodec = CA.Record.object "PursGraphNode" | ||
{ depends: CA.array moduleNameCodec | ||
, path: CA.string | ||
} | ||
|
||
-- | A module name string from a 'purs graph' invocation. | ||
newtype ModuleName = ModuleName String | ||
|
||
derive instance Newtype ModuleName _ | ||
derive instance Eq ModuleName | ||
derive instance Ord ModuleName | ||
|
||
moduleNameCodec :: JsonCodec ModuleName | ||
moduleNameCodec = Profunctor.wrapIso ModuleName CA.string | ||
|
||
type AssociatedError = { module :: ModuleName, path :: FilePath, error :: String } | ||
|
||
-- | Given a function to parse the `path` component of `purs graph`, associate | ||
-- | all associate all modules in the groph with their package names. | ||
associateModules :: (FilePath -> Either String PackageName) -> PursGraph -> Either (NonEmptyArray AssociatedError) (Map ModuleName PackageName) | ||
associateModules parse graph = do | ||
let | ||
parsed :: Array (Either AssociatedError (Tuple ModuleName PackageName)) | ||
parsed = Map.toUnfoldableUnordered graph # map \(Tuple moduleName { path }) -> parse path # bimap | ||
(\error -> { module: moduleName, path, error }) | ||
(\packageName -> Tuple moduleName packageName) | ||
|
||
separated :: { errors :: Array AssociatedError, values :: Array (Tuple ModuleName PackageName) } | ||
separated = parsed # Array.foldMap case _ of | ||
Left err -> { errors: [ err ], values: [] } | ||
Right tup -> { errors: [], values: [ tup ] } | ||
|
||
case NonEmptyArray.fromArray separated.errors of | ||
Nothing -> pure $ Map.fromFoldable separated.values | ||
Just errors -> Left errors | ||
|
||
-- | Find direct dependencies of the given module, according to the given graph. | ||
directDependencies :: ModuleName -> PursGraph -> Maybe (Set ModuleName) | ||
directDependencies name = map (Set.fromFoldable <<< _.depends) <<< Map.lookup name | ||
|
||
-- | Find all dependencies of the given module, according to the given graph. | ||
allDependencies :: ModuleName -> PursGraph -> Maybe (Set ModuleName) | ||
allDependencies start graph = map Set.fromFoldable (getDependencies start) | ||
where | ||
getDependencies name = | ||
map _.depends (Map.lookup name graph) >>= case _ of | ||
[] -> pure [] | ||
directs -> do | ||
let nextDeps = map Array.concat (traverse getDependencies directs) | ||
nextDeps <> Just directs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unused dependency; since we solve for it, though, we need to add it to the local registry, registry-index, and storage fixtures (hence the other added files).