Share windows 11 below source code build success case

Viewed 144

system: win11
go: 1.20.14
answer: 1.2.5
editor: vs code

go proxy: https://goproxy.cn,direct

The most, most most most ... important thing is errors:

  1. q1

  2. q2

  3. See the question comments section above

  4. The root cause of the problem is that the copy file code in Windows is faulty, Q3 also results from this situation

My solution was to modify buid.go in the source code

  1. add 2 func

func CopyDir(src, dst string, ignoreDir []string) (count int, err error) {
	// 使用正则表达式将路径按照 / 或 \ 分割成切片
	regexp1, err := regexp.Compile(`(/|\\)`)
	if err != nil {
		return 0, err
	}
	srcSplits := regexp1.Split(src, 10000)
	dstSplits := regexp1.Split(dst, 10000)
	// 调用 CopyDirInner 函数,传入源文件夹和目标文件夹的前缀和最后一级名称
	return CopyDirInner(strings.Join(srcSplits[:len(srcSplits)-1], "/"), srcSplits[len(srcSplits)-1], strings.Join(dstSplits[:len(dstSplits)-1], "/"), "", ignoreDir)
}

// CopyDirInner 函数接受四个字符串参数,分别表示源文件夹和目标文件夹的前缀和最后一级名称
// 返回值是拷贝的文件数量和可能发生的错误
func CopyDirInner(srcPrefix, src string, dstPrefix, dst string, ignoreDir []string) (count int, err error) {

	ignoreThisDir := func(path string) bool {
		for _, s := range ignoreDir {
			if strings.HasPrefix(path, s) {
				return true
			}
		}
		return false
	}

	// 如果前缀为空,则设置为当前目录
	if srcPrefix == "" {
		srcPrefix = "."
	}
	if dstPrefix == "" {
		dstPrefix = "."
	}
	// 读取源文件夹下的所有文件和子文件夹
	dirs, err := os.ReadDir(srcPrefix + "/" + src)
	if err != nil {
		return 0, err
	}

	if ignoreThisDir(srcPrefix) {
		return 0, nil
	}

	// 在目标文件夹下创建同名的子文件夹
	pathCursor := dstPrefix + "/" + dst + "/" + src
	err = os.MkdirAll(pathCursor, 0600)
	if err != nil {
		return 0, err
	}
	for _, dir := range dirs {
		if dir.IsDir() {
			// 如果是子文件夹,则递归调用 CopyDirInner 函数,传入相应的参数
			countSub, err := CopyDirInner(srcPrefix+"/"+src, dir.Name(), dstPrefix+"/"+dst, src, ignoreDir)
			if err != nil {
				return 0, err
			}
			count += countSub
		} else {
			// 如果是文件,则读取其内容,并写入到目标文件夹下同名的文件中
			bytesFile, err := os.ReadFile(srcPrefix + "/" + src + "/" + dir.Name())
			if err != nil {
				return 0, err
			}

			err = os.WriteFile(pathCursor+"/"+dir.Name(), bytesFile, 0600)
			if err != nil {
				return 0, err
			}
			count++
		}
	}
	return count, nil
}
  1. edit copyDirEntries func
// Add parameters and throw the original path over
func copyDirEntries(sourceFsDir string, sourceFs fs.FS, sourceDir, targetDir string, ignoreDir ...string) (err error) {
	err = dir.CreateDirIfNotExist(targetDir)
	if err != nil {
		return err
	}
	// ignoreThisDir := func(path string) bool {
	// 	for _, s := range ignoreDir {
	// 		if strings.HasPrefix(path, s) {
	// 			return true
	// 		}
	// 	}
	// 	return false
	// }
    
    // add code
	srcPath := filepath.Join(sourceFsDir, sourceDir)
	fmt.Println(srcPath, targetDir)
	_, err = CopyDir(srcPath, targetDir, ignoreDir)
	
    // The following code is completely removed
	// err = fs.WalkDir(sourceFs, sourceDir, func(path string, d fs.DirEntry, err error) error {
	// 	if err != nil {
	// 		return err
	// 	}
	// 	if ignoreThisDir(path) {
	// 		return nil
	// 	}

	// 	// Convert the path to use forward slashes, important because we use embedded FS which always uses forward slashes
	// 	path = filepath.ToSlash(path)

	// 	// Construct the absolute path for the source file/directory
	// 	srcPath := filepath.Join(sourceFsDir, sourceDir, path)

	// 	// Construct the absolute path for the destination file/directory
	// 	dstPath := filepath.Join(targetDir, path)

	// 	if d.IsDir() {
	// 		// Create the directory in the destination
	// 		err := os.MkdirAll(dstPath, os.ModePerm)
	// 		if err != nil {
	// 			return fmt.Errorf("failed to create directory %s: %w", dstPath, err)
	// 		}
	// 	} else {

	// 		// // Open the source file
	// 		// srcFile, err := sourceFs.Open(srcPath)
	// 		// if err != nil {
	// 		// 	return fmt.Errorf("failed to open source file %s: %w", srcPath, err)
	// 		// }
	// 		// defer srcFile.Close()

	// 		// // Create the destination file
	// 		// dstFile, err := os.Create(dstPath)
	// 		// if err != nil {
	// 		// 	return fmt.Errorf("failed to create destination file %s: %w", dstPath, err)
	// 		// }
	// 		// defer dstFile.Close()

	// 		// // Copy the file contents
	// 		// _, err = io.Copy(dstFile, srcFile)
	// 		// if err != nil {
	// 		// 	return fmt.Errorf("failed to copy file contents from %s to %s: %w", srcPath, dstPath, err)
	// 		// }
	// 	}

	// 	return nil
	// })

	return err
}
  1. Change where the function copyDirEntries is called

func copyUIFiles

func copyUIFiles(b *buildingMaterial) (err error) {
	// .... line 231
	// The node_modules folder generated during development will interfere packaging, so it needs to be ignored.
	if err = copyDirEntries(goModUIDir, os.DirFS(goModUIDir), ".", localUIBuildDir, "node_modules"); err != nil {
		return fmt.Errorf("failed to copy ui files: %w", err)
	}
        // ....
    for _, entry := range pluginsDirEntries {
		// .... line 263
		if err = copyDirEntries("", os.DirFS(sourcePluginDir), ".", localPluginDir); err != nil {
			return fmt.Errorf("failed to copy ui files: %w", err)
		}
	}
    // ....
}

func replaceNecessaryFile

func replaceNecessaryFile(b *buildingMaterial) (err error) {
	// .... line 336
	err = copyDirEntries("", ui.Build, ".", uiBuildDir)
	// ....
}

cmd

go run .\cmd\answer\main.go build --with github.com/apache/incubator-answer-plugins/user-center-wecom@latest
1 Answers

image.png

wa sai ~

do one last go build
image.png