How to use clean method of build Package

Best Syzkaller code snippet using build.clean

clean.go

Source:clean.go Github

copy

Full Screen

1// Copyright 2012 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4// Package clean implements the ``go clean'' command.5package clean6import (7 "context"8 "fmt"9 "io"10 "os"11 "path/filepath"12 "strconv"13 "strings"14 "time"15 "cmd/go/internal/base"16 "cmd/go/internal/cache"17 "cmd/go/internal/cfg"18 "cmd/go/internal/load"19 "cmd/go/internal/lockedfile"20 "cmd/go/internal/modfetch"21 "cmd/go/internal/modload"22 "cmd/go/internal/work"23)24var CmdClean = &base.Command{25 UsageLine: "go clean [clean flags] [build flags] [packages]",26 Short: "remove object files and cached files",27 Long: `28Clean removes object files from package source directories.29The go command builds most objects in a temporary directory,30so go clean is mainly concerned with object files left by other31tools or by manual invocations of go build.32If a package argument is given or the -i or -r flag is set,33clean removes the following files from each of the34source directories corresponding to the import paths:35 _obj/ old object directory, left from Makefiles36 _test/ old test directory, left from Makefiles37 _testmain.go old gotest file, left from Makefiles38 test.out old test log, left from Makefiles39 build.out old test log, left from Makefiles40 *.[568ao] object files, left from Makefiles41 DIR(.exe) from go build42 DIR.test(.exe) from go test -c43 MAINFILE(.exe) from go build MAINFILE.go44 *.so from SWIG45In the list, DIR represents the final path element of the46directory, and MAINFILE is the base name of any Go source47file in the directory that is not included when building48the package.49The -i flag causes clean to remove the corresponding installed50archive or binary (what 'go install' would create).51The -n flag causes clean to print the remove commands it would execute,52but not run them.53The -r flag causes clean to be applied recursively to all the54dependencies of the packages named by the import paths.55The -x flag causes clean to print remove commands as it executes them.56The -cache flag causes clean to remove the entire go build cache.57The -testcache flag causes clean to expire all test results in the58go build cache.59The -modcache flag causes clean to remove the entire module60download cache, including unpacked source code of versioned61dependencies.62The -fuzzcache flag causes clean to remove files stored in the Go build63cache for fuzz testing. The fuzzing engine caches files that expand64code coverage, so removing them may make fuzzing less effective until65new inputs are found that provide the same coverage. These files are66distinct from those stored in testdata directory; clean does not remove67those files.68For more about build flags, see 'go help build'.69For more about specifying packages, see 'go help packages'.70 `,71}72var (73 cleanI bool // clean -i flag74 cleanR bool // clean -r flag75 cleanCache bool // clean -cache flag76 cleanFuzzcache bool // clean -fuzzcache flag77 cleanModcache bool // clean -modcache flag78 cleanTestcache bool // clean -testcache flag79)80func init() {81 // break init cycle82 CmdClean.Run = runClean83 CmdClean.Flag.BoolVar(&cleanI, "i", false, "")84 CmdClean.Flag.BoolVar(&cleanR, "r", false, "")85 CmdClean.Flag.BoolVar(&cleanCache, "cache", false, "")86 CmdClean.Flag.BoolVar(&cleanFuzzcache, "fuzzcache", false, "")87 CmdClean.Flag.BoolVar(&cleanModcache, "modcache", false, "")88 CmdClean.Flag.BoolVar(&cleanTestcache, "testcache", false, "")89 // -n and -x are important enough to be90 // mentioned explicitly in the docs but they91 // are part of the build flags.92 work.AddBuildFlags(CmdClean, work.DefaultBuildFlags)93}94func runClean(ctx context.Context, cmd *base.Command, args []string) {95 // golang.org/issue/29925: only load packages before cleaning if96 // either the flags and arguments explicitly imply a package,97 // or no other target (such as a cache) was requested to be cleaned.98 cleanPkg := len(args) > 0 || cleanI || cleanR99 if (!modload.Enabled() || modload.HasModRoot()) &&100 !cleanCache && !cleanModcache && !cleanTestcache && !cleanFuzzcache {101 cleanPkg = true102 }103 if cleanPkg {104 for _, pkg := range load.PackagesAndErrors(ctx, load.PackageOpts{}, args) {105 clean(pkg)106 }107 }108 var b work.Builder109 b.Print = fmt.Print110 if cleanCache {111 dir := cache.DefaultDir()112 if dir != "off" {113 // Remove the cache subdirectories but not the top cache directory.114 // The top cache directory may have been created with special permissions115 // and not something that we want to remove. Also, we'd like to preserve116 // the access log for future analysis, even if the cache is cleared.117 subdirs, _ := filepath.Glob(filepath.Join(dir, "[0-9a-f][0-9a-f]"))118 printedErrors := false119 if len(subdirs) > 0 {120 if cfg.BuildN || cfg.BuildX {121 b.Showcmd("", "rm -r %s", strings.Join(subdirs, " "))122 }123 if !cfg.BuildN {124 for _, d := range subdirs {125 // Only print the first error - there may be many.126 // This also mimics what os.RemoveAll(dir) would do.127 if err := os.RemoveAll(d); err != nil && !printedErrors {128 printedErrors = true129 base.Errorf("go: %v", err)130 }131 }132 }133 }134 logFile := filepath.Join(dir, "log.txt")135 if cfg.BuildN || cfg.BuildX {136 b.Showcmd("", "rm -f %s", logFile)137 }138 if !cfg.BuildN {139 if err := os.RemoveAll(logFile); err != nil && !printedErrors {140 printedErrors = true141 base.Errorf("go: %v", err)142 }143 }144 }145 }146 if cleanTestcache && !cleanCache {147 // Instead of walking through the entire cache looking for test results,148 // we write a file to the cache indicating that all test results from before149 // right now are to be ignored.150 dir := cache.DefaultDir()151 if dir != "off" {152 f, err := lockedfile.Edit(filepath.Join(dir, "testexpire.txt"))153 if err == nil {154 now := time.Now().UnixNano()155 buf, _ := io.ReadAll(f)156 prev, _ := strconv.ParseInt(strings.TrimSpace(string(buf)), 10, 64)157 if now > prev {158 if err = f.Truncate(0); err == nil {159 if _, err = f.Seek(0, 0); err == nil {160 _, err = fmt.Fprintf(f, "%d\n", now)161 }162 }163 }164 if closeErr := f.Close(); err == nil {165 err = closeErr166 }167 }168 if err != nil {169 if _, statErr := os.Stat(dir); !os.IsNotExist(statErr) {170 base.Errorf("go: %v", err)171 }172 }173 }174 }175 if cleanModcache {176 if cfg.GOMODCACHE == "" {177 base.Fatalf("go: cannot clean -modcache without a module cache")178 }179 if cfg.BuildN || cfg.BuildX {180 b.Showcmd("", "rm -rf %s", cfg.GOMODCACHE)181 }182 if !cfg.BuildN {183 if err := modfetch.RemoveAll(cfg.GOMODCACHE); err != nil {184 base.Errorf("go: %v", err)185 }186 }187 }188 if cleanFuzzcache {189 fuzzDir := cache.Default().FuzzDir()190 if cfg.BuildN || cfg.BuildX {191 b.Showcmd("", "rm -rf %s", fuzzDir)192 }193 if !cfg.BuildN {194 if err := os.RemoveAll(fuzzDir); err != nil {195 base.Errorf("go: %v", err)196 }197 }198 }199}200var cleaned = map[*load.Package]bool{}201// TODO: These are dregs left by Makefile-based builds.202// Eventually, can stop deleting these.203var cleanDir = map[string]bool{204 "_test": true,205 "_obj": true,206}207var cleanFile = map[string]bool{208 "_testmain.go": true,209 "test.out": true,210 "build.out": true,211 "a.out": true,212}213var cleanExt = map[string]bool{214 ".5": true,215 ".6": true,216 ".8": true,217 ".a": true,218 ".o": true,219 ".so": true,220}221func clean(p *load.Package) {222 if cleaned[p] {223 return224 }225 cleaned[p] = true226 if p.Dir == "" {227 base.Errorf("%v", p.Error)228 return229 }230 dirs, err := os.ReadDir(p.Dir)231 if err != nil {232 base.Errorf("go: %s: %v", p.Dir, err)233 return234 }235 var b work.Builder236 b.Print = fmt.Print237 packageFile := map[string]bool{}238 if p.Name != "main" {239 // Record which files are not in package main.240 // The others are.241 keep := func(list []string) {242 for _, f := range list {243 packageFile[f] = true244 }245 }246 keep(p.GoFiles)247 keep(p.CgoFiles)248 keep(p.TestGoFiles)249 keep(p.XTestGoFiles)250 }251 _, elem := filepath.Split(p.Dir)252 var allRemove []string253 // Remove dir-named executable only if this is package main.254 if p.Name == "main" {255 allRemove = append(allRemove,256 elem,257 elem+".exe",258 p.DefaultExecName(),259 p.DefaultExecName()+".exe",260 )261 }262 // Remove package test executables.263 allRemove = append(allRemove,264 elem+".test",265 elem+".test.exe",266 p.DefaultExecName()+".test",267 p.DefaultExecName()+".test.exe",268 )269 // Remove a potential executable, test executable for each .go file in the directory that270 // is not part of the directory's package.271 for _, dir := range dirs {272 name := dir.Name()273 if packageFile[name] {274 continue275 }276 if dir.IsDir() {277 continue278 }279 if strings.HasSuffix(name, "_test.go") {280 base := name[:len(name)-len("_test.go")]281 allRemove = append(allRemove, base+".test", base+".test.exe")282 }283 if strings.HasSuffix(name, ".go") {284 // TODO(adg,rsc): check that this .go file is actually285 // in "package main", and therefore capable of building286 // to an executable file.287 base := name[:len(name)-len(".go")]288 allRemove = append(allRemove, base, base+".exe")289 }290 }291 if cfg.BuildN || cfg.BuildX {292 b.Showcmd(p.Dir, "rm -f %s", strings.Join(allRemove, " "))293 }294 toRemove := map[string]bool{}295 for _, name := range allRemove {296 toRemove[name] = true297 }298 for _, dir := range dirs {299 name := dir.Name()300 if dir.IsDir() {301 // TODO: Remove once Makefiles are forgotten.302 if cleanDir[name] {303 if cfg.BuildN || cfg.BuildX {304 b.Showcmd(p.Dir, "rm -r %s", name)305 if cfg.BuildN {306 continue307 }308 }309 if err := os.RemoveAll(filepath.Join(p.Dir, name)); err != nil {310 base.Errorf("go: %v", err)311 }312 }313 continue314 }315 if cfg.BuildN {316 continue317 }318 if cleanFile[name] || cleanExt[filepath.Ext(name)] || toRemove[name] {319 removeFile(filepath.Join(p.Dir, name))320 }321 }322 if cleanI && p.Target != "" {323 if cfg.BuildN || cfg.BuildX {324 b.Showcmd("", "rm -f %s", p.Target)325 }326 if !cfg.BuildN {327 removeFile(p.Target)328 }329 }330 if cleanR {331 for _, p1 := range p.Internal.Imports {332 clean(p1)333 }334 }335}336// removeFile tries to remove file f, if error other than file doesn't exist337// occurs, it will report the error.338func removeFile(f string) {339 err := os.Remove(f)340 if err == nil || os.IsNotExist(err) {341 return342 }343 // Windows does not allow deletion of a binary file while it is executing.344 if base.ToolIsWindows {345 // Remove lingering ~ file from last attempt.346 if _, err2 := os.Stat(f + "~"); err2 == nil {...

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(filepath.Clean(path))4}5import (6func main() {7 fmt.Println(filepath.Base(path))8}9import (10func main() {11 fmt.Println(filepath.Dir(path))12}13import (14func main() {15 fmt.Println(filepath.Split(path))16}17import (18func main() {19 fmt.Println(filepath.SplitList(path))20}21import (22func main() {23 fmt.Println(filepath.Join("a", "b", "c"))24}25import (26func main() {27 fmt.Println(filepath.Join("a", "b", "c", "d.go"))28}29import (30func main() {31 fmt.Println(filepath.Join("a", "b", "c", "d.go"))32}

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(filepath.Clean("/a/./b/c/"))4 fmt.Println(filepath.Clean("/a/../b/c/"))5 fmt.Println(filepath.Clean("/a/b/c/.."))6 fmt.Println(filepath.Clean("/a/b/c/../"))7}8import (9func main() {10 fmt.Println(filepath.Dir("/a/b/c"))11 fmt.Println(filepath.Dir("/a/b/c/"))12 fmt.Println(filepath.Dir("/a/b/c/."))13 fmt.Println(filepath.Dir("/a/b/c/.."))14 fmt.Println(filepath.Dir("/a/b/c/../.."))15}16import (17func main() {18 fmt.Println(filepath.EvalSymlinks("/a/b/c"))19}20import (21func main() {22 fmt.Println(filepath.Ext("/a/b/c.go"))23 fmt.Println(filepath.Ext("/a/b/c"))24}25import (26func main() {27 fmt.Println(filepath.FromSlash("/a/b/c"))28 fmt.Println(filepath.FromSlash("\\a\\b\\c"))29}30import (31func main() {32 fmt.Println(filepath.IsAbs("/a/b/c"))33 fmt.Println(filepath.IsAbs("a/b/c"))34}35import (36func main() {37 fmt.Println(filepath.Join("/a/b/c", "

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(build.Default.GOPATH)4 fmt.Println(build.Default.GOROOT)5 fmt.Println(build.Default.GOROOT_FINAL)6 fmt.Println(build.Default.GOARCH)7 fmt.Println(build.Default.GOOS)8 fmt.Println(build.Default.GOHOSTARCH)9 fmt.Println(build.Default.GOHOSTOS)10 fmt.Println(build.Default.CgoEnabled)11 fmt.Println(build.Default.CgoEnabled)12 fmt.Println(build.Default.GOPATH)13 fmt.Println(build.Default.GOROOT)14 fmt.Println(build.Default.GOARCH)15 fmt.Println(build.Default.GOOS)16 fmt.Println(build.Default.GOHOSTARCH)17 fmt.Println(build.Default.GOHOSTOS)18 fmt.Println(build.Default.GOROOT_FINAL)19 fmt.Println(build.Default.UseAllFiles)20 fmt.Println(build.Default.Compiler)21 fmt.Println(build.Default.InstallSuffix)22 fmt.Println(build.Default.BuildTags)23 fmt.Println(build.Default.ReleaseTags)24 fmt.Println(build.Default.CgoEnabled)25 fmt.Println(build.Default.DefaultGOROOT)26 fmt.Println(build.Default.DefaultGOPATH)27 fmt.Println(build.Default.SplitPathList)28 fmt.Println(build.Default.IsAbsPath)29 fmt.Println(build.Default.IsLocalImport)30 fmt.Println(build.Default.HasSubdir)31 fmt.Println(build.Default.IsDir)32 fmt.Println(build.Default.IsFile)33 fmt.Println(build.Default.ReadDir)34 fmt.Println(build.Default.OpenFile)35 fmt.Println(build.Default.Import)36 fmt.Println(build.Default.ImportDir)37 fmt.Println(build.Default.ImportFrom)38 fmt.Println(build.Default.ImportComment)39 fmt.Println(build.Default.Context)40 fmt.Println(build.Default.ImportPaths)41 fmt.Println(build.Default.ImportHash)42 fmt.Println(build.Default.MatchFile)43 fmt.Println(build.Default.Match)44 fmt.Println(build.Default.Context)45 fmt.Println(build.Default.Context)46 fmt.Println(bu

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(filepath.Clean("/home/ashish/go/src/ashish"))4 fmt.Println(filepath.Clean("/home/ashish/go/src/ashish/"))5 fmt.Println(filepath.Clean("/home/ashish/go/../go/src/ashish/"))6}7import (8func main() {9 fmt.Println(filepath.Dir("/home/ashish/go/src/ashish"))10 fmt.Println(filepath.Dir("/home/ashish/go/src/ashish/"))11 fmt.Println(filepath.Dir("/home/ashish/go/../go/src/ashish/"))12}13import (14func main() {15 fmt.Println(filepath.EvalSymlinks("/home/ashish/go/src"))16 fmt.Println(filepath.EvalSymlinks("/home/ashish/go/src/ashish"))17}18import (19func main() {20 fmt.Println(filepath.IsAbs("/home/ashish/go/src/ashish"))21 fmt.Println(filepath.IsAbs("home/ashish/go/src/ashish"))22}23import (24func main() {25 fmt.Println(filepath.Join("/home/ashish/go/src/ashish", "hello"))26 fmt.Println(filepath.Join("/home/ashish/go/src/ashish/", "hello"))27 fmt.Println(filepath.Join("/home/ashish/go/src/ashish", "/hello"))28 fmt.Println(filepath.Join("/home/ashish/go/src/ashish/", "/hello"))29}

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(path)4 fmt.Println(filepath.Clean(path))5}6import (7func main() {8 fmt.Println(path)9 fmt.Println(filepath.Clean(path))10 fmt.Println(filepath.Join(path, "test"))11}12import (13func main() {14 fmt.Println(path)15 fmt.Println(filepath.Clean(path))16 fmt.Println(filepath.Join(path, "test"))17 fmt.Println(filepath.Split(path))18}19import (20func main() {21 fmt.Println(path)22 fmt.Println(filepath.Clean(path))23 fmt.Println(filepath.Join(path, "test"))24 fmt.Println(filepath.Split(path))25 _, file := filepath.Split(path)26 fmt.Println(file)27}28import (29func main() {30 fmt.Println(path)31 fmt.Println(filepath.Clean(path))32 fmt.Println(filepath.Join(path, "test"))33 fmt.Println(filepath.Split(path))34 _, file := filepath.Split(path)35 fmt.Println(file)36 fmt.Println(filepath.Ext(file))37}

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(filepath.Clean(path))4}5import (6func main() {7 fmt.Println(filepath.Split(path))8}9import (10func main() {11 fmt.Println(filepath.Join(path))12}13import (14func main() {15 fmt.Println(filepath.IsAbs(path))16}17import (18func main() {19 fmt.Println(filepath.Abs(path))20}21import (22func main() {23 fmt.Println(filepath.SplitList(path))24}25import (

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(filepath.Clean(p))4}5import (6func main() {7 fmt.Println(filepath.Join(p))8}9import (10func main() {11 fmt.Println(filepath.Split(p))12}13import (14func main() {15 fmt.Println(filepath.SplitList(p))16}

Full Screen

Full Screen

clean

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4import (5func main() {6}7import (8func main() {9}10import (11func main() {12}13import (14func main() {15}16import (17func main() {18}19import (20func main() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful