Search code examples
intellij-ideagoland

Fake go environment from a shell script


I am using ultimate with goland plugin and dep. I used to work outside GOPATH as well. Usually without idea I am using a shell script and Makefile to build project and was so far happy with it. Idea with dep integration of course forces me to work inside GOPATH/src. Is there any way to configure that IDE to run a shell script which will fake project environment, set GOPATH, GOBIN and make dep happy and get rid of ... is not within a known GOPATH/src and "missing" imports.

I have tried to run it via ./build/env.sh idea . but still getting missing imports and ... is not within a known GOPATH/src. Tried to play with project settings as well, seems nothing can satisfy that gep.

env.sh

#!/bin/sh

set -e

if [ ! -f "build/env.sh" ]; then
    echo "$0 must be run from the root of the repository."
    exit 2
fi

project="proj"
repositoryRoot="domain.com/username"

# Create fake Go workspace if it doesn't exist yet.
workspace="$PWD/build/_workspace"
root="$PWD"
dir="$workspace/src/$repositoryRoot"
if [ ! -L "$dir/$project" ]; then
    mkdir -p "$dir"
    cd "$dir"
    ln -s ../../../../../. $project
    cd "$root"
fi

# Set up the environment to use the workspace.
# Also add Godeps workspace so we build using canned dependencies.
GOPATH="$workspace"
GOBIN="$PWD/build/bin"

# Run the command inside the workspace.
cd "$dir/$project"
PWD="$dir/$project"

# Launch the arguments with the configured environment.
exec "$@"

Makefile

.PHONY: all test clean

GOBIN = build/bin

all:
    build/env.sh go get github.com/golang/dep/cmd/dep
    build/env.sh $(GOBIN)/dep ensure
    build/env.sh go build -v -o $(GOBIN)/proj

Solution

  • The short answer is no, you cannot. GoLand/IntelliJ IDEA Ultimate with the Go plugin cannot work in the desired way. They respect the Go Workspace layout, and you should structure your code as such.

    If you switch from dep to Go Modules, then you'll be able to create your project anywhere and not have the same restrictions as the GOPATH mode has. Here you can read more about working with Go Modules.