buildExtractedTarball :: (StackM env m, HasEnvConfig env, MonadBaseUnlift IO m) => Path Abs Dir -> m ()
buildExtractedTarball pkgDir = do
  projectRoot <- view projectRootL
  envConfig <- view envConfigL
  menv <- getMinimalEnvOverride
  localPackageToBuild <- readLocalPackage pkgDir
  let packageEntries = bcPackageEntries (envConfigBuildConfig envConfig)
      getPaths entry = do
        resolvedEntry <- resolvePackageEntry menv projectRoot entry
        return $ fmap fst resolvedEntry
  allPackagePaths <- fmap mconcat (mapM getPaths packageEntries)
  -- We remove the path based on the name of the package
  let isPathToRemove path = do
        localPackage <- readLocalPackage path
        return $ packageName (lpPackage localPackage) == packageName (lpPackage localPackageToBuild)
  pathsToRemove <- filterM isPathToRemove allPackagePaths
  let adjustPackageEntries entries path = do
        adjustedPackageEntries <- mapM (removePathFromPackageEntry menv projectRoot path) entries
        return (catMaybes adjustedPackageEntries)
  entriesWithoutBuiltPackage <- foldM adjustPackageEntries packageEntries pathsToRemove
  let newEntry = PackageEntry Nothing (PLFilePath (toFilePath pkgDir)) []
  newPackagesRef <- liftIO (newIORef Nothing)
  let adjustEnvForBuild env =
        let updatedEnvConfig = envConfig
              {envConfigPackagesRef = newPackagesRef
              ,envConfigBuildConfig = updatePackageInBuildConfig (envConfigBuildConfig envConfig)
              }
        in set envConfigL updatedEnvConfig env
      updatePackageInBuildConfig buildConfig = buildConfig
        { bcPackageEntries = newEntry : entriesWithoutBuiltPackage
        , bcConfig = (bcConfig buildConfig)
                     { configBuild = defaultBuildOpts
                       { boptsTests = True
                       }
                     }
        }
  local adjustEnvForBuild $
    build (const (return ())) Nothing defaultBuildOptsCLI

-- | Version of 'checkSDistTarball' that first saves lazy bytestring to
-- temporary directory and then calls 'checkSDistTarball' on it.
buildExtractedTarball :: (StackM env m, HasEnvConfig env, MonadBaseUnlift IO m) => Path Abs Dir -> m ()
buildExtractedTarball pkgDir = do
  projectRoot <- view projectRootL
  envConfig <- view envConfigL
  menv <- getMinimalEnvOverride
  localPackageToBuild <- readLocalPackage pkgDir
  let packageEntries = bcPackageEntries (envConfigBuildConfig envConfig)
      getPaths entry = do
        resolvedEntry <- resolvePackageEntry menv projectRoot entry
        return $ fmap fst resolvedEntry
  allPackagePaths <- fmap mconcat (mapM getPaths packageEntries)
  -- We remove the path based on the name of the package
  let isPathToRemove path = do
        localPackage <- readLocalPackage path
        return $ packageName (lpPackage localPackage) == packageName (lpPackage localPackageToBuild)
  pathsToRemove <- filterM isPathToRemove allPackagePaths
  let adjustPackageEntries entries path = do
        adjustedPackageEntries <- mapM (removePathFromPackageEntry menv projectRoot path) entries
        return (catMaybes adjustedPackageEntries)
  entriesWithoutBuiltPackage <- foldM adjustPackageEntries packageEntries pathsToRemove
  let newEntry = PackageEntry Nothing (PLFilePath (toFilePath pkgDir)) []
  newPackagesRef <- liftIO (newIORef Nothing)
  let adjustEnvForBuild env =
        let updatedEnvConfig = envConfig
              {envConfigPackagesRef = newPackagesRef
              ,envConfigBuildConfig = updatePackageInBuildConfig (envConfigBuildConfig envConfig)
              }
        in set envConfigL updatedEnvConfig env
      updatePackageInBuildConfig buildConfig = buildConfig
        { bcPackageEntries = newEntry : entriesWithoutBuiltPackage
        , bcConfig = (bcConfig buildConfig)
                     { configBuild = defaultBuildOpts
                       { boptsTests = True
                       }
                     }
        }
  local adjustEnvForBuild $
    build (const (return ())) Nothing defaultBuildOptsCLI

-- | Version of 'checkSDistTarball' that first saves lazy bytestring to
-- temporary directory and then calls 'checkSDistTarball' on it.
buildExtractedTarball pkgDir = do
  projectRoot <- view projectRootL
  envConfig <- view envConfigL
  menv <- getMinimalEnvOverride
  localPackageToBuild <- readLocalPackage pkgDir
  let packageEntries = bcPackageEntries (envConfigBuildConfig envConfig)
      getPaths entry = do
        resolvedEntry <- resolvePackageEntry menv projectRoot entry
        return $ fmap fst resolvedEntry
  allPackagePaths <- fmap mconcat (mapM getPaths packageEntries)
  -- We remove the path based on the name of the package
  let isPathToRemove path = do
        localPackage <- readLocalPackage path
        return $ packageName (lpPackage localPackage) == packageName (lpPackage localPackageToBuild)
  pathsToRemove <- filterM isPathToRemove allPackagePaths
  let adjustPackageEntries entries path = do
        adjustedPackageEntries <- mapM (removePathFromPackageEntry menv projectRoot path) entries
        return (catMaybes adjustedPackageEntries)
  entriesWithoutBuiltPackage <- foldM adjustPackageEntries packageEntries pathsToRemove
  let newEntry = PackageEntry Nothing (PLFilePath (toFilePath pkgDir)) []
  newPackagesRef <- liftIO (newIORef Nothing)
  let adjustEnvForBuild env =
        let updatedEnvConfig = envConfig
              {envConfigPackagesRef = newPackagesRef
              ,envConfigBuildConfig = updatePackageInBuildConfig (envConfigBuildConfig envConfig)
              }
        in set envConfigL updatedEnvConfig env
      updatePackageInBuildConfig buildConfig = buildConfig
        { bcPackageEntries = newEntry : entriesWithoutBuiltPackage
        , bcConfig = (bcConfig buildConfig)
                     { configBuild = defaultBuildOpts
                       { boptsTests = True
                       }
                     }
        }
  local adjustEnvForBuild $
    build (const (return ())) Nothing defaultBuildOptsCLI

-- | Version of 'checkSDistTarball' that first saves lazy bytestring to
-- temporary directory and then calls 'checkSDistTarball' on it.