Snyk Golang CLI Plugin
The `snyk-go-plugin` package serves as a crucial plugin for the Snyk CLI, enabling it to detect and report known vulnerabilities within Golang projects. It specifically supports projects utilizing `dep` (via `Gopkg.lock`) or `govendor` (via `vendor/vendor.json`) for dependency management, as well as `go.mod` projects. The current stable version is 2.1.1, with releases occurring frequently, often monthly or bi-monthly, and sometimes more often for critical bug fixes. Its primary differentiator is its integration with the broader Snyk security platform, providing automated vulnerability scanning and remediation advice for Go applications, unlike standalone static analysis tools. It ships with TypeScript types, indicating strong support for modern JavaScript and TypeScript development workflows, and requires Node.js version 20 or higher.
Common errors
-
Error: Command failed: go mod graph
cause The plugin couldn't execute `go mod graph` (or similar Go commands) successfully, likely due to a misconfigured Go environment, an invalid `go.mod` file, or missing Go installation.fixEnsure Go is correctly installed and accessible in your system's PATH. Verify that `go mod graph` runs without errors in your project directory. Check your `go.mod` for syntax errors or unresolvable modules. -
TypeError: Cannot read properties of undefined (reading 'dependencies')
cause The `inspect` function returned an unexpected or incomplete result object, possibly due to a problem parsing the Go project's dependencies or an internal plugin error.fixInspect the full `result` object returned by `inspect` to understand its structure. Ensure the project path and manifest file provided to `inspect` are correct. Enable debug logging for the plugin if available to get more detailed error information. -
Error: Cannot find module '@snyk/dep-graph'
cause The internal dependency `@snyk/dep-graph` is missing, often due to an incomplete `npm install` or issues with module resolution.fixRun `npm install` or `yarn install` again in your project to ensure all dependencies are correctly installed. Clear your `node_modules` and package manager cache if the issue persists.
Warnings
- breaking The `inspect` function now generates PackageURLs (purl) by default. This changes the structure of the output object, adding a 'purl' field to package objects.
- gotcha This package is a plugin designed to be used with the Snyk CLI tool, not as a standalone application for direct vulnerability scanning. While it provides a programmatic API, its primary context is within the Snyk ecosystem.
- gotcha The plugin relies on Go's module system. Projects with complex `replace` directives in `go.mod` (especially those pointing to local paths) may lead to incorrect dependency graphs.
- gotcha The package requires Node.js version 20 or higher. Running it with older Node.js versions will result in execution errors.
Install
-
npm install snyk-go-plugin -
yarn add snyk-go-plugin -
pnpm add snyk-go-plugin
Imports
- inspect
const inspect = require('snyk-go-plugin').inspect;import { inspect } from 'snyk-go-plugin'; - InspectOptions
import type { InspectOptions } from 'snyk-go-plugin';
Quickstart
import { inspect } from 'snyk-go-plugin';
import * as path from 'path';
async function scanGoProject(projectDir: string) {
try {
console.log(`Scanning Go project in: ${projectDir}`);
// The 'go.mod' file is often used as the manifest file, or Gopkg.lock/vendor/vendor.json
const result = await inspect(
projectDir,
'go.mod',
{
// Optionally disable PackageURL generation if not needed
configuration: { includePackageUrls: true },
// Other options can be passed here, e.g., debug: true
}
);
console.log('Snyk Go Plugin inspection result:');
console.dir(result, { depth: null });
if (result.package.dependencies) {
console.log(`Found ${Object.keys(result.package.dependencies).length} direct dependencies.`);
}
} catch (error) {
console.error('Error during Snyk Go Plugin inspection:', error);
process.exit(1);
}
}
// Example usage: scan the current directory as a Go project
scanGoProject(process.cwd());