Skip to content
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

make namespace.dependencies ignore lib #4509

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/unison-util-relation/src/Unison/Util/Relation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,13 @@ lookupDom' x r = M.lookup x (domain r)
lookupRan' :: (Ord b) => b -> Relation a b -> Maybe (Set a)
lookupRan' y r = M.lookup y (range r)

-- | True if the element @ x @ exists in the domain of @ r @.
-- | True if the element exists in the domain.
memberDom :: (Ord a) => a -> Relation a b -> Bool
memberDom x r = isJust $ lookupDom' x r
memberDom x r = M.member x (domain r)

-- | True if the element exists in the range.
memberRan :: (Ord b) => b -> Relation a b -> Bool
memberRan y r = isJust $ lookupRan' y r
memberRan y r = M.member y (range r)

filterDom :: (Ord a, Ord b) => (a -> Bool) -> Relation a b -> Relation a b
filterDom f r = S.filter f (dom r) <| r
Expand Down
6 changes: 6 additions & 0 deletions unison-cli/src/Unison/Cli/MonadUtils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Unison.Cli.MonadUtils
getLastSavedRootHash,
setLastSavedRootHash,
getMaybeBranchAt,
getMaybeBranch0At,
expectBranchAtPath,
expectBranchAtPath',
expectBranch0AtPath,
Expand Down Expand Up @@ -291,6 +292,11 @@ getMaybeBranchAt path = do
rootBranch <- getRootBranch
pure (Branch.getAt (Path.unabsolute path) rootBranch)

-- | Get the maybe-branch0 at an absolute path.
getMaybeBranch0At :: Path.Absolute -> Cli (Maybe (Branch0 IO))
getMaybeBranch0At path =
fmap Branch.head <$> getMaybeBranchAt path

-- | Get the branch at a relative path, or return early if there's no such branch.
expectBranchAtPath :: Path -> Cli (Branch IO)
expectBranchAtPath =
Expand Down
13 changes: 2 additions & 11 deletions unison-cli/src/Unison/Codebase/Editor/HandleInput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import Unison.Codebase.Editor.HandleInput.MoveAll (handleMoveAll)
import Unison.Codebase.Editor.HandleInput.MoveBranch (doMoveBranch)
import Unison.Codebase.Editor.HandleInput.MoveTerm (doMoveTerm)
import Unison.Codebase.Editor.HandleInput.MoveType (doMoveType)
import Unison.Codebase.Editor.HandleInput.NamespaceDependencies qualified as NamespaceDependencies
import Unison.Codebase.Editor.HandleInput.NamespaceDependencies (handleNamespaceDependencies)
import Unison.Codebase.Editor.HandleInput.NamespaceDiffUtils (diffHelper)
import Unison.Codebase.Editor.HandleInput.ProjectClone (handleClone)
import Unison.Codebase.Editor.HandleInput.ProjectCreate (projectCreate)
Expand Down Expand Up @@ -1177,16 +1177,7 @@ loop e = do
PushRemoteBranchI pushRemoteBranchInput -> handlePushRemoteBranch pushRemoteBranchInput
ListDependentsI hq -> handleDependents hq
ListDependenciesI hq -> handleDependencies hq
NamespaceDependenciesI namespacePath' -> do
Cli.Env {codebase} <- ask
path <- maybe Cli.getCurrentPath Cli.resolvePath' namespacePath'
Cli.getMaybeBranchAt path >>= \case
Nothing -> Cli.respond $ BranchEmpty (WhichBranchEmptyPath (Path.absoluteToPath' path))
Just b -> do
externalDependencies <-
Cli.runTransaction (NamespaceDependencies.namespaceDependencies codebase (Branch.head b))
ppe <- PPE.unsuffixifiedPPE <$> currentPrettyPrintEnvDecl Backend.Within
Cli.respondNumbered $ ListNamespaceDependencies ppe path externalDependencies
NamespaceDependenciesI path -> handleNamespaceDependencies path
DebugNumberedArgsI -> do
numArgs <- use #numberedArgs
Cli.respond (DumpNumberedArgs numArgs)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
module Unison.Codebase.Editor.HandleInput.NamespaceDependencies
( namespaceDependencies,
( handleNamespaceDependencies,
)
where

import Control.Lens (over)
import Control.Monad.Reader (ask)
import Control.Monad.Trans.Maybe
import Data.Map qualified as Map
import Data.Set qualified as Set
import Unison.Cli.Monad qualified as Cli
import Unison.Cli.MonadUtils qualified as Cli
import Unison.Cli.PrettyPrintUtils (currentPrettyPrintEnvDecl)
import Unison.Codebase (Codebase)
import Unison.Codebase qualified as Codebase
import Unison.Codebase.Branch (Branch0)
import Unison.Codebase.Branch qualified as Branch
import Unison.Codebase.Editor.Output qualified as Output
import Unison.Codebase.Path qualified as Path
import Unison.DataDeclaration qualified as DD
import Unison.LabeledDependency (LabeledDependency)
import Unison.LabeledDependency qualified as LD
import Unison.Name (Name)
import Unison.Name qualified as Name
import Unison.Prelude
import Unison.Reference (Reference)
import Unison.PrettyPrintEnvDecl qualified as PPED
import Unison.Reference qualified as Reference
import Unison.Referent (Referent)
import Unison.Referent qualified as Referent
import Unison.Server.Backend qualified as Backend
import Unison.Sqlite qualified as Sqlite
import Unison.Symbol (Symbol)
import Unison.Term qualified as Term
import Unison.Util.Relation qualified as Relation
import Unison.Util.Relation3 qualified as Relation3
import Unison.Util.Relation4 qualified as Relation4

handleNamespaceDependencies :: Maybe Path.Path' -> Cli.Cli ()
handleNamespaceDependencies namespacePath' = do
Cli.Env {codebase} <- ask
path <- maybe Cli.getCurrentPath Cli.resolvePath' namespacePath'
branch <-
Cli.getMaybeBranch0At path & onNothingM do
Cli.returnEarly (Output.BranchEmpty (Output.WhichBranchEmptyPath (Path.absoluteToPath' path)))
externalDependencies <-
Cli.runTransaction (namespaceDependencies codebase branch)
ppe <- PPED.unsuffixifiedPPE <$> currentPrettyPrintEnvDecl Backend.Within
Cli.respondNumbered $ Output.ListNamespaceDependencies ppe path externalDependencies

-- | Check the dependencies of all types, terms, and metadata in the current namespace,
-- returns a map of dependencies which do not have a name within the current namespace,
Expand All @@ -41,56 +59,36 @@ import Unison.Util.Relation4 qualified as Relation4
namespaceDependencies :: Codebase m Symbol a -> Branch0 m -> Sqlite.Transaction (Map LabeledDependency (Set Name))
namespaceDependencies codebase branch = do
typeDeps <-
for (Map.toList currentBranchTypeRefs) $ \(typeRef, names) -> fmap (fromMaybe Map.empty) . runMaybeT $ do
refId <- MaybeT . pure $ Reference.toId typeRef
decl <- MaybeT $ Codebase.getTypeDeclaration codebase refId
let typeDeps = Set.map LD.typeRef $ DD.typeDependencies (DD.asDataDecl decl)
pure $ foldMap (`Map.singleton` names) typeDeps
for (Map.toList (Relation.domain (Branch.deepTypes branchWithoutLibdeps))) \(typeRef, names) ->
fmap (fromMaybe Map.empty) . runMaybeT $ do
refId <- MaybeT . pure $ Reference.toId typeRef
decl <- MaybeT $ Codebase.getTypeDeclaration codebase refId
let typeDeps = Set.map LD.typeRef $ DD.typeDependencies (DD.asDataDecl decl)
pure $ foldMap (`Map.singleton` names) typeDeps

termDeps <- for (Map.toList currentBranchTermRefs) $ \(termRef, names) -> fmap (fromMaybe Map.empty) . runMaybeT $ do
refId <- MaybeT . pure $ Referent.toReferenceId termRef
term <- MaybeT $ Codebase.getTerm codebase refId
let termDeps = Term.labeledDependencies term
pure $ foldMap (`Map.singleton` names) termDeps
termDeps <-
for (Map.toList (Relation.domain (Branch.deepTerms branchWithoutLibdeps))) \(termRef, names) ->
fmap (fromMaybe Map.empty) . runMaybeT $ do
refId <- MaybeT . pure $ Referent.toReferenceId termRef
term <- MaybeT $ Codebase.getTerm codebase refId
let termDeps = Term.labeledDependencies term
pure $ foldMap (`Map.singleton` names) termDeps

let dependenciesToDependents :: Map LabeledDependency (Set Name)
dependenciesToDependents =
Map.unionsWith (<>) (metadata : typeDeps ++ termDeps)
Map.unionsWith (<>) (typeDeps ++ termDeps)

let onlyExternalDeps :: Map LabeledDependency (Set Name)
onlyExternalDeps =
Map.filterWithKey
( \x _ ->
LD.fold
(`Map.notMember` currentBranchTypeRefs)
(`Map.notMember` currentBranchTermRefs)
(\k -> not (Relation.memberDom k (Branch.deepTypes branch)))
(\k -> not (Relation.memberDom k (Branch.deepTerms branch)))
x
)
dependenciesToDependents

pure onlyExternalDeps
where
currentBranchTermRefs :: Map Referent (Set Name)
currentBranchTermRefs = Relation.domain (Branch.deepTerms branch)
currentBranchTypeRefs :: Map Reference (Set Name)
currentBranchTypeRefs = Relation.domain (Branch.deepTypes branch)

-- Since metadata is only linked by reference, not by name,
-- it's possible that the metadata itself is external to the branch.
metadata :: Map LabeledDependency (Set Name)
metadata =
let typeMetadataRefs :: Map LabeledDependency (Set Name)
typeMetadataRefs =
(Branch.deepTypeMetadata branch)
& Relation4.d234 -- Select only the type and value portions of the metadata
& \rel ->
let types = Map.mapKeys LD.typeRef $ Relation.range (Relation3.d12 rel)
terms = Map.mapKeys LD.termRef $ Relation.range (Relation3.d13 rel)
in Map.unionWith (<>) types terms
termMetadataRefs :: Map LabeledDependency (Set Name)
termMetadataRefs =
(Branch.deepTermMetadata branch)
& Relation4.d234 -- Select only the type and value portions of the metadata
& \rel ->
let types = Map.mapKeys LD.typeRef $ Relation.range (Relation3.d12 rel)
terms = Map.mapKeys LD.termRef $ Relation.range (Relation3.d13 rel)
in Map.unionWith (<>) types terms
in Map.unionWith (<>) typeMetadataRefs termMetadataRefs
branchWithoutLibdeps = branch & over Branch.children (Map.delete Name.libSegment)
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ hasMetadata = 3
3. dependsOnNat
4. hasMetadata

builtin.Text 4. hasMetadata

builtin.Nat.drop 2. dependsOnIntAndNat

metadata.myMetadata 4. hasMetadata

```
Loading