From 0c7215307a530ddf31851742298fe39d1627957f Mon Sep 17 00:00:00 2001 From: Chase Fleming <1666730+chasefleming@users.noreply.github.com> Date: Thu, 2 May 2024 13:51:36 -0700 Subject: [PATCH] Add message if conflicting contract on dependency install --- .../dependencymanager/dependencyinstaller.go | 48 ++++++++++++++++--- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/internal/dependencymanager/dependencyinstaller.go b/internal/dependencymanager/dependencyinstaller.go index 101526bf2..5faebe292 100644 --- a/internal/dependencymanager/dependencyinstaller.go +++ b/internal/dependencymanager/dependencyinstaller.go @@ -50,6 +50,7 @@ import ( type categorizedLogs struct { fileSystemActions []string stateUpdates []string + issues []string } func (cl *categorizedLogs) LogAll(logger output.Logger) { @@ -59,7 +60,7 @@ func (cl *categorizedLogs) LogAll(logger output.Logger) { if len(cl.fileSystemActions) > 0 { logger.Info(util.MessageWithEmojiPrefix("🗃️", "File System Actions:")) for _, msg := range cl.fileSystemActions { - logger.Info(util.MessageWithEmojiPrefix("✅", msg)) + logger.Info(msg) } logger.Info("") // Add a line break after the section } @@ -67,11 +68,19 @@ func (cl *categorizedLogs) LogAll(logger output.Logger) { if len(cl.stateUpdates) > 0 { logger.Info(util.MessageWithEmojiPrefix("💾", "State Updates:")) for _, msg := range cl.stateUpdates { - logger.Info(util.MessageWithEmojiPrefix("✅", msg)) + logger.Info(msg) } logger.Info("") // Add a line break after the section } + if len(cl.issues) > 0 { + logger.Info(util.MessageWithEmojiPrefix("⚠️", "Issues:")) + for _, msg := range cl.issues { + logger.Info(msg) + } + logger.Info("") + } + if len(cl.fileSystemActions) == 0 && len(cl.stateUpdates) == 0 { logger.Info(util.MessageWithEmojiPrefix("👍", "Zero changes were made. Everything looks good.")) } @@ -280,7 +289,8 @@ func (di *DependencyInstaller) handleFileSystem(contractAddr, contractName, cont return fmt.Errorf("failed to create contract file: %w", err) } - di.logs.fileSystemActions = append(di.logs.fileSystemActions, fmt.Sprintf("%s from %s on %s installed", contractName, contractAddr, networkName)) + msg := util.MessageWithEmojiPrefix("✅️", fmt.Sprintf("Contract %s from %s on %s installed", contractName, contractAddr, networkName)) + di.logs.fileSystemActions = append(di.logs.fileSystemActions, msg) } return nil @@ -297,6 +307,20 @@ func isCoreContract(contractName string) bool { return false } +// checkForContractConflicts checks if a contract with the same name already exists in the state and adds a warning +func (di *DependencyInstaller) checkForContractConflicts(contractName string) error { + _, err := di.State.Contracts().ByName(contractName) + if err != nil { + return nil + } else { + if !isCoreContract(contractName) { + msg := util.MessageWithEmojiPrefix("❌", fmt.Sprintf("Contract named %s already exists in flow.json", contractName)) + di.logs.issues = append(di.logs.issues, msg) + } + return nil + } +} + func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, assignedName, contractName string, program *project.Program) error { hash := sha256.New() hash.Write(program.CodeWithUnprocessedImports()) @@ -324,7 +348,14 @@ func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, as } } - err := di.handleFileSystem(contractAddr, contractName, contractData, networkName) + //// This needs to happen before dependency state is updated + err := di.checkForContractConflicts(assignedName) + if err != nil { + di.Logger.Error(fmt.Sprintf("Error checking for contract conflicts: %v", err)) + return err + } + + err = di.handleFileSystem(contractAddr, contractName, contractData, networkName) if err != nil { return fmt.Errorf("error handling file system: %w", err) } @@ -343,7 +374,8 @@ func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, as return err } - di.logs.stateUpdates = append(di.logs.stateUpdates, fmt.Sprintf("%s added to emulator deployments", contractName)) + msg := util.MessageWithEmojiPrefix("✅", fmt.Sprintf("%s added to emulator deployments", contractName)) + di.logs.stateUpdates = append(di.logs.stateUpdates, msg) } // If the contract is not a core contract and the user does not want to skip aliasing, then prompt for an alias @@ -354,7 +386,8 @@ func (di *DependencyInstaller) handleFoundContract(networkName, contractAddr, as return err } - di.logs.stateUpdates = append(di.logs.stateUpdates, fmt.Sprintf("Alias added for %s on %s", contractName, networkName)) + msg := util.MessageWithEmojiPrefix("✅", fmt.Sprintf("Alias added for %s on %s", contractName, networkName)) + di.logs.stateUpdates = append(di.logs.stateUpdates, msg) } return nil @@ -424,7 +457,8 @@ func (di *DependencyInstaller) updateDependencyState(networkName, contractAddres di.State.Contracts().AddDependencyAsContract(dep, networkName) if isNewDep { - di.logs.stateUpdates = append(di.logs.stateUpdates, fmt.Sprintf("%s added to flow.json", dep.Name)) + msg := util.MessageWithEmojiPrefix("✅", fmt.Sprintf("%s added to flow.json", dep.Name)) + di.logs.stateUpdates = append(di.logs.stateUpdates, msg) } return nil