Skip to content

Commit

Permalink
Merge pull request #1208 from joakim-noah/next
Browse files Browse the repository at this point in the history
Fix #895, so that dub looks for a compiler next to itself first
  • Loading branch information
joakim-noah authored Dec 6, 2017
2 parents 93a2132 + 2ad4038 commit bf095d8
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 12 deletions.
45 changes: 42 additions & 3 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -1182,17 +1182,56 @@ class Dub {

private void determineDefaultCompiler()
{
import std.range : front;
import std.file : thisExePath;
import std.path : buildPath, dirName, expandTilde, isAbsolute, isDirSeparator;
import std.process : environment;
import std.range : front;
import std.regex : ctRegex, matchFirst;

m_defaultCompiler = m_config.defaultCompiler.expandTilde;
if (m_defaultCompiler.length && m_defaultCompiler.isAbsolute)
return;

auto dubPrefix = m_defaultCompiler.matchFirst(ctRegex!(`^\$DUB_BINARY_PATH`));
if(!dubPrefix.empty)
{
m_defaultCompiler = thisExePath().dirName() ~ dubPrefix.post;
return;
}

m_defaultCompiler = m_config.defaultCompiler;
if (m_defaultCompiler.length) return;
if (!find!isDirSeparator(m_defaultCompiler).empty)
throw new Exception("defaultCompiler specified in a DUB config file cannot use an unqualified relative path:\n\n" ~ m_defaultCompiler ~
"\n\nUse \"$DUB_BINARY_PATH/../path/you/want\" instead.");

version (Windows) enum sep = ";", exe = ".exe";
version (Posix) enum sep = ":", exe = "";

auto compilers = ["dmd", "gdc", "gdmd", "ldc2", "ldmd2"];
// If a compiler name is specified, look for it next to dub.
// Otherwise, look for any of the common compilers adjacent to dub.
if (m_defaultCompiler.length)
{
string compilerPath = buildPath(thisExePath().dirName(), m_defaultCompiler ~ exe);
if (existsFile(compilerPath))
{
m_defaultCompiler = compilerPath;
return;
}
}
else
{
auto nextFound = compilers.find!(bin => existsFile(buildPath(thisExePath().dirName(), bin ~ exe)));
if (!nextFound.empty)
{
m_defaultCompiler = buildPath(thisExePath().dirName(), nextFound.front ~ exe);
return;
}
}

// If nothing found next to dub, search the user's PATH, starting
// with the compiler name from their DUB config file, if specified.
if (m_defaultCompiler.length)
compilers = m_defaultCompiler ~ compilers;
auto paths = environment.get("PATH", "").splitter(sep).map!NativePath;
auto res = compilers.find!(bin => paths.canFind!(p => existsFile(p ~ (bin~exe))));
m_defaultCompiler = res.empty ? compilers[0] : res.front;
Expand Down
72 changes: 63 additions & 9 deletions test/issue895-local-configuration.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#!/usr/bin/env bash
. $(dirname "${BASH_SOURCE[0]}")/common.sh

cd ${CURR_DIR}
mkdir ../etc
mkdir ../etc/dub
echo "{\"defaultCompiler\": \"foo\"}" > ../etc/dub/settings.json

if [ -e /var/lib/dub/settings.json ]; then
die $LINENO 'Found existing system wide DUB configuration. Aborting.'
fi
Expand All @@ -14,9 +9,68 @@ if [ -e ~/.dub/settings.json ]; then
die $LINENO 'Found existing user wide DUB configuration. Aborting.'
fi

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF 'Unknown compiler: foo'; then
rm -r ../etc
die $LINENO 'DUB did not find the local configuration'
cd ${CURR_DIR}
mkdir -p ../etc/dub
echo "{\"defaultCompiler\": \"foo\"}" > ../etc/dub/settings.json
echo "Empty file named foo." > ../bin/foo

function cleanup {
rm -r ../etc
}

trap cleanup EXIT

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "Unknown compiler: $(dirname $CURR_DIR)/bin/foo"; then
rm ../bin/foo
die $LINENO 'DUB did not find the local configuration with an adjacent compiler.'
fi

echo "{\"defaultCompiler\": \"$CURR_DIR/foo\"}" > ../etc/dub/settings.json
mv ../bin/foo $CURR_DIR

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "Unknown compiler: $CURR_DIR/foo"; then
rm $CURR_DIR/foo
die $LINENO 'DUB did not find a locally-configured compiler with an absolute path.'
fi

echo "{\"defaultCompiler\": \"~/.dub/foo\"}" > ../etc/dub/settings.json
mv $CURR_DIR/foo ~/.dub/

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "Unknown compiler: "; then
rm ~/.dub/foo
die $LINENO 'DUB did not find a locally-configured compiler with a tilde-prefixed path.'
fi

echo "{\"defaultCompiler\": \"\$DUB_BINARY_PATH/../foo\"}" > ../etc/dub/settings.json
mv ~/.dub/foo ..

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "Unknown compiler: $(dirname $CURR_DIR)/bin/../foo"; then
rm ../foo
die $LINENO 'DUB did not find a locally-configured compiler with a DUB-relative path.'
fi

echo "{\"defaultCompiler\": \"../foo\"}" > ../etc/dub/settings.json

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "defaultCompiler specified in a DUB config file cannot use an unqualified relative path"; then
rm ../foo
die $LINENO 'DUB did not error properly for a locally-configured compiler with a relative path.'
fi

rm ../etc/dub/settings.json
echo "Empty file named ldc2." > ../bin/ldc2

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "Failed to invoke the compiler $(dirname $CURR_DIR)/bin/ldc2 to determine the build platform"; then
rm ../bin/ldc2
die $LINENO 'DUB did not find ldc2 adjacent to it.'
fi

echo "{\"defaultCompiler\": \"foo\"}" > ../etc/dub/settings.json
rm ../bin/ldc2
export PATH=$(dirname $CURR_DIR)${PATH:+:$PATH}

if ! { ${DUB} describe --single issue103-single-file-package.d 2>&1 || true; } | grep -cF "Unknown compiler: foo"; then
rm ../foo
die $LINENO 'DUB did not find a locally-configured compiler in its PATH.'
fi

rm -r ../etc
rm ../foo

0 comments on commit bf095d8

Please sign in to comment.