解决go mod vendor 报错io/fs is not in GOROOT问题

在Linux环境下开发Go项目时,遇到使用viper v1.8.1版本时,由于该版本依赖Go 1.16.5,而当前系统中是Go 1.15.8,导致gomodvendor执行失败。解决方案包括升级Go版本至1.16.5或降级viper版本。升级步骤涉及下载新版本、更新环境变量并验证版本变更,然后重新执行gomodvendor。另一种方案是寻找与现有Go版本兼容的viper低版本。

问题背景

在linux 系统下开发go项目


项目场景:

在linux 系统下开发go项目,当前go版本: go1.15.8

项目中需要使用viper组件,viper版本v1.8.1

go项目使用go mod 模式编译


问题描述:

1.执行go mod vendor时报错:

github.com/spf13/viper imports

github.com/spf13/afero imports

io/fs: package io/fs is not in GOROOT(usr/local/go/src/io/fs)

go build 


原因分析:

viper v1.8.1依赖更高版本的go版本: v1.16.5
 


解决方案:

方案一:

1.下载go版本v1.16.5

2.解压到指定目录,如$HOME

3.编辑$HOME/.bashrc ,添加:

        export PATH=$HOME/go/bin:$PATH

4.生效:

        source ~/.bashrc

5. 查看go 版本是否是v1.16.5

        go version

6.重新执行go mod vendor,问题得到解决。

方案二:

使用更低版本的viper。

  

root@ubuntu:/home/uu/go/src/github.com/hyperledger/fabric-samples/test-network# sudo ./network.sh deployCC Using docker and docker-compose deploying chaincode on channel 'mychannel' executing with the following - CHANNEL_NAME: mychannel - CC_NAME: basic - CC_SRC_PATH: ../asset-transfer-basic/chaincode-go - CC_SRC_LANGUAGE: go - CC_VERSION: 1.0 - CC_SEQUENCE: auto - CC_END_POLICY: NA - CC_COLL_CONFIG: NA - CC_INIT_FCN: NA - DELAY: 3 - MAX_RETRY: 5 - VERBOSE: false executing with the following - CC_NAME: basic - CC_SRC_PATH: ../asset-transfer-basic/chaincode-go - CC_SRC_LANGUAGE: go - CC_VERSION: 1.0 Vendoring Go dependencies at ../asset-transfer-basic/chaincode-go /home/uu/go/src/github.com/hyperledger/fabric-samples/asset-transfer-basic/chaincode-go /home/uu/go/src/github.com/hyperledger/fabric-samples/test-network /home/uu/go/src/github.com/hyperledger/fabric-samples/test-network Finished vendoring Go dependencies + '[' false = true ']' + peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go --lang golang --label basic_1.0 + res=0 Chaincode is packaged Installing chaincode on peer0.org1... Using organization 1 + peer lifecycle chaincode queryinstalled --output json + jq -r 'try (.installed_chaincodes[].package_id)' + grep '^Perform chaincode operations: package|install|queryinstalled|getinstalledpackage|approveformyorg|queryapproved|checkcommitreadiness|commit|querycommitted Usage: peer lifecycle chaincode [command] Available Commands: approveformyorg Approve the chaincode definition for my org. checkcommitreadiness Check whether a chaincode definition is ready to be committed on a channel. commit Commit the chaincode definition on the channel. getinstalledpackage Get an installed chaincode package from a peer. install Install a chaincode. package Package a chaincode queryapproved Query an org'\''s approved chaincode definition from its peer. querycommitted Query the committed chaincode definitions by channel on a peer. queryinstalled Query the installed chaincodes on a peer. Flags: --cafile string Path to file containing PEM-encoded trusted certificate(s) for the ordering endpoint --certfile string Path to file containing PEM-encoded X509 public key to use for mutual TLS communication with the orderer endpoint --clientauth Use mutual TLS when communicating with the orderer endpoint --connTimeout duration Timeout for client to connect (default 3s) -h, --help help for chaincode --keyfile string Path to file containing PEM-encoded private key to use for mutual TLS communication with the orderer endpoint -o, --orderer string Ordering service endpoint --ordererTLSHostnameOverride string The hostname override to use when validating the TLS connection to the orderer --tls Use TLS when communicating with the orderer endpoint --tlsHandshakeTimeShift duration The amount of time to shift backwards for certificate expiration checks during TLS handshakes with the orderer endpoint Use "peer lifecycle chaincode [command] --help" for more information about a command.$' + test 1 -ne 0 + peer lifecycle chaincode install basic.tar.gz + res=1 Error: chaincode install failed with status: 500 - failed to invoke backing implementation of 'InstallChaincode': could not build chaincode: docker build failed: docker image build failed: docker build failed: Error returned from build: 1 "vendor/google.golang.org/protobuf/internal/editiondefaults/defaults.go:9:8: cannot find package "." in: /chaincode/input/src/vendor/embed vendor/golang.org/x/net/http2/transport.go:19:2: cannot find package "." in: /chaincode/input/src/vendor/io/fs vendor/google.golang.org/grpc/stats/metrics.go:19:8: cannot find package "." in: /chaincode/input/src/vendor/maps vendor/google.golang.org/grpc/balancer/pickfirst/internal/internal.go:22:2: cannot find package "." in: /chaincode/input/src/vendor/math/rand/v2 vendor/google.golang.org/grpc/balancer/pickfirst/pickfirstleaf/pickfirstleaf.go:33:2: cannot find package "." in: /chaincode/input/src/vendor/net/netip vendor/google.golang.org/grpc/clientconn.go:27:2: cannot find package "." in: /chaincode/input/src/vendor/slices " Chaincode installation on peer0.org1 has failed Deploying chaincode failed
06-12
<think>好的,我需要解决用户遇到的“cp: target binary/vendor/etc/firmware/ is not a directory”错误。这个错误通常在使用cp命令复制文件时出现,目标路径不存在或者不是一个目录。让我一步步分析可能的原因和解决方法。 首先,用户可能在使用cp命令时,目标路径的某个部分不存在。例如,如果目标路径是binary/vendor/etc/firmware/,但binary目录下没有vendor子目录,或者vendor下没有etc,依此类推,就会导致这个错误。这时候,系统无法找到对应的目录,因此报错。 接下来,我需要确认用户使用的命令格式是否正确。cp命令的基本语法是cp [选项] 源文件 目标路径。如果用户试图将多个文件复制到一个不存在的目录,而没有使用递归选项,也可能出现这个问题。例如,如果用户执行的是cp file1 file2 target/,而target/不存在,就会报错解决方法之一应该是检查目标路径是否存在。用户可以使用mkdir -p命令递归创建所有缺失的目录。例如,mkdir -p binary/vendor/etc/firmware/,然后再执行cp命令。这样确保目标路径存在。 另外,用户可能想将文件复制到一个新目录,但忘记添加-r或-a选项,特别是当复制整个目录时。不过,在这个错误中,问题更可能是路径不存在,而不是选项的问题。 还有一种情况是,目标路径中存在拼写错误或者路径结构错误。例如,用户可能误将binary写成了bin,或者路径中的斜杠方向不对(比如使用了反斜杠\而不是正斜杠/)。需要检查路径的正确性。 用户提供的引用中提到了Net booting和apks文件夹的提取,可能他们在进行某种系统镜像的配置或刷机操作,可能涉及到文件系统的目录结构。例如,在构建或提取镜像时,目录结构可能未正确创建,导致复制文件时目标路径缺失。引用[2]提到网络启动可以避免频繁刷机,可能用户是在测试内核或文件系统的修改,这时候确保目标目录存在尤为重要。 总结解决方法步骤: 1. 检查目标路径是否存在,逐级确认每个目录是否存在。 2. 使用mkdir -p创建缺失的目录。 3. 确保cp命令的目标路径正确无误,没有拼写错误。 4. 如果复制多个文件,确保目标路径存在且是目录。 然后,用户可能需要生成相关问题,比如关于cp命令的其他错误,目录创建,或者路径验证的方法。例如: 1. 如何递归创建多级目录? 2. Linux中如何检查文件或目录是否存在? 3. cp命令的其他常见错误及解决方法? 最后,确保回答结构清晰,分步骤说明,并且引用提供的资料中的相关内容,比如在解释网络启动时提到的避免频繁刷机的情况,可能用户在进行相关操作时需要特别注意目录结构的正确性,防止设备变砖。</think>### 解决`cp`命令报错“target binary/vendor/etc/firmware/ is not a directory” 此错误表示目标路径不存在或路径中的某些目录层级缺失。以下是分步解决方案: --- #### **1. 验证目标路径是否存在** 使用`ls`命令逐级检查路径中的每个目录是否存在: ```bash ls binary/ # 检查binary目录是否存在 ls binary/vendor/ # 检查vendor子目录是否存在 ls binary/vendor/etc/ # 检查etc目录是否存在 ... ``` 若某一层级缺失,需手动创建目录。 --- #### **2. 递归创建缺失的目录** 使用`mkdir -p`自动创建所有缺失的目录层级: ```bash mkdir -p binary/vendor/etc/firmware/ ``` - `-p`参数表示递归创建父目录,即使中间目录不存在也会一并创建[^2]。 --- #### **3. 确认`cp`命令语法正确** 确保复制命令的格式为: ```bash cp 源文件 目标路径 ``` 例如: ```bash cp firmware_image.bin binary/vendor/etc/firmware/ ``` 若需复制整个目录,需添加`-r`参数: ```bash cp -r source_folder/ binary/vendor/etc/firmware/ ``` --- #### **4. 检查路径拼写与权限** - **路径拼写**:确认路径分隔符为`/`(Linux系统),而非`\`。 - **权限问题**:若目录权限不足,使用`sudo`提升权限: ```bash sudo mkdir -p binary/vendor/etc/firmware/ sudo cp firmware_image.bin binary/vendor/etc/firmware/ ``` --- #### **5. 特殊情况:路径中的空格或特殊字符** 若路径包含空格或特殊符号(如`*`, `?`),需用引号包裹路径: ```bash cp "source file.txt" "target path with space/" ``` --- ### 相关问题 1. 如何在Linux中递归复制整个目录? 2. `mkdir -p`和普通`mkdir`有何区别? 3. 如何解决“Permission denied”文件权限错误? 4. Linux中如何验证文件路径是否存在?[^1] --- ### 引用说明 - 引用[^2]提到网络启动可避免频繁刷机,此时需确保文件系统路径正确,防止操作失败导致设备变砖。 - 引用中提取APKs文件夹的操作可能涉及类似目录创建问题,需注意路径完整性。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值