drgm51600 2019-06-10 13:15
浏览 48
已采纳

gorename:什么是“请勿编辑”标记?

I'm using vscode and trying to rename a variable name.

It fails saying:

Rename failed: gorename: cannot rename identifiers in generated file containing DO NOT EDIT marker: /home/adam/go/src/hello/hello.go 

So what is a DO NOT EDIT marker? Why is it there and how can I remove it so that gorename can do its thing?

This is how my file begins:

package main

// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include "c/greet.h"
import "C"
import (
    "encoding/json"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)
  • 写回答

1条回答 默认 最新

  • donglei1699 2019-06-10 13:33
    关注

    According to the source code for gorename, the check is simple: It looks for a comment at the beginning of the line that contains the phrase "DO NOT EDIT":

    // Matches cgo generated comment as well as the proposed standard:
    //  https://golang.org/s/generatedcode
    var generatedRx = regexp.MustCompile(`// .*DO NOT EDIT\.?`)
    
    // generated reports whether ast.File is a generated file.
    func generated(f *ast.File, tokenFile *token.File) bool {
    
        // Iterate over the comments in the file
        for _, commentGroup := range f.Comments {
            for _, comment := range commentGroup.List {
                if matched := generatedRx.MatchString(comment.Text); matched {
                    // Check if comment is at the beginning of the line in source
                    if pos := tokenFile.Position(comment.Slash); pos.Column == 1 {
                        return true
                    }
                }
            }
        }
        return false
    }
    

    Reading elsewhere in the source code, where the error message itself is generated, it looks like the error message may simply be misleading:

    // Renaming not supported if cgo files are affected.
    var generatedFileNames []string
    for _, info := range r.packages {
        for _, f := range info.Files {
            tokenFile := r.iprog.Fset.File(f.Pos())
            if filesToUpdate[tokenFile] && generated(f, tokenFile) {
                generatedFileNames = append(generatedFileNames, tokenFile.Name())
            }
        }
    }
    if !Force && len(generatedFileNames) > 0 {
        return fmt.Errorf("refusing to modify generated file%s containing DO NOT EDIT marker: %v", plural(len(generatedFileNames)), generatedFileNames)
    }
    

    I see you are using CGO. It appears that this also triggers the "generated file" marker.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题