修复此代码中 Windows 'filename too long' CI 测试失败的最简洁方法?

Cleanest way to fix Windows 'filename too long' CI tests failure in this code?

我正在尝试解决this Windows filename issue

基本上我们的 CI 作业失败了,出现 Windows 的 'filename too long' 错误。

 warning: Could not stat path 'node_modules/data-validator/tests/data/data-examples/ds000247/sub-emptyroom/ses-18910512/meg/sub-emptyroom_ses-18910512_task-noise_run-01_meg.ds/sub-emptyroom_ses-18910512_task-noise_run-01_meg.acq': Filename too long

我已经阅读了 Node 路径模块的文档,这似乎是一个可能的解决方案。我还读到了 Windows 前缀 (\?\) 来绕过 MAX_PATH...但不知道如何以干净的方式实现这些。

这部分代码库包含失败的测试。硬编码路径 (testDatasetPath) 可能是问题的一部分。

function getDirectories(srcpath) {
  return fs.readdirSync(srcpath).filter(function(file) {
    return (
      file !== '.git' && fs.statSync(path.join(srcpath, file)).isDirectory()
    )
  })
}

var missing_session_files = //array of strings here

const dataDirectory = 'data-validator/tests/data/'

function createDatasetFileList(path) {
  const testDatasetPath = `${dataDirectory}${path}`
  if (!isNode) {
    return createFileList(testDatasetPath)
  } else {
    return testDatasetPath
  }
}

创建文件列表函数

function createFileList(dir) {
  const str = dir.substr(dir.lastIndexOf('/') + 1) + '$'
  const rootpath = dir.replace(new RegExp(str), '')
  const paths = getFilepaths(dir, [], rootpath)
  return paths.map(path => {
    return createFile(path, path.replace(rootpath, ''))
  })
}

tl;dr GitLab CI 作业在 Windows 上失败,因为节点模块文件名变得太长。我怎样才能使这个 nodejs 代码 OS 不可知?

这是 Windows 环境中的一个已知错误,但是,有一个修复程序..

如果您使用的是基于 NTFS 的文件系统,您应该能够在

中启用长路径
Local Computer Policy > Computer Configuration > Administrative Templates > System > Filesystem > NTFS

这也在您刚刚链接的文档中指定,理论上应该可行。但是,路径实际上不应超过 32 位,

这会带来一些性能影响,但应该可以完成工作。

此外,您最好切换到更好的包或搜索替代品。如果这是你自己的包裹,也许重组它?

最后,如果 none 这些工作,请将您的项目文件夹直接移动到您的数据驱动器,(C:\) 这将减少其他嵌套和父文件夹。

这本身就很糟糕,如果您选择这样做,您可能 运行 在部署过程中遇到问题。