Skip to content

Commit

Permalink
Fix bug and add regression tests (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
alcuadrado authored Sep 18, 2019
1 parent 429efa0 commit 3e7301a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 13 deletions.
18 changes: 7 additions & 11 deletions packages/buidler-core/src/internal/util/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ export function lazyFunction<T extends Function>(functionCreator: () => T): T {
);
}

const creationsStack: boolean[] = [];

function createLazyProxy<ActualT extends GuardT, GuardT extends object>(
targetCreator: () => ActualT,
dummyTargetCreator: () => GuardT,
Expand All @@ -77,10 +75,7 @@ function createLazyProxy<ActualT extends GuardT, GuardT extends object>(

function getRealTarget(): ActualT {
if (realTarget === undefined) {
creationsStack.push(true);
const target = targetCreator();
creationsStack.pop();

validator(target);

// We copy all properties. We won't use them, but help us avoid Proxy
Expand Down Expand Up @@ -122,16 +117,17 @@ function createLazyProxy<ActualT extends GuardT, GuardT extends object>(
// created, it would trigger an endless loop of recreation, which node
// detects and resolve to an empty object.
//
// This happens with Web3.js because we a lazyFunction that loads it,
// and expose it as `global.Web3`. This Web3.js file accesses
// `global.Web3` when it's being loaded, triggering the loop we mentioned
// This happens with Web3.js because we a lazyObject that loads it,
// and expose it as `global.web3`. This Web3.js file accesses
// `global.web3` when it's being loaded, triggering the loop we mentioned
// before: /~https://github.com/ethereum/web3.js/blob/8574bd3bf11a2e9cf4bcf8850cab13e1db56653f/packages/web3-core-requestmanager/src/givenProvider.js#L41
//
// We just return `undefined` in that case, to not enter into the loop.
const stack = new Error().stack;
if (
creationsStack.length > 0 &&
realTarget === undefined &&
property === "currentProvider"
stack !== undefined &&
stack.includes("givenProvider.js") &&
realTarget === undefined
) {
return undefined;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/buidler-web3-legacy/scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ else
start_ganache
fi

../../node_modules/.bin/mocha --exit
../../node_modules/.bin/mocha --exit

node web3-lazy-object-tests/when-accessing-web3-class.js
node web3-lazy-object-tests/when-accessing-web3-object.js
node web3-lazy-object-tests/when-requiring-web3-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { lazyFunction, lazyObject } = require("@nomiclabs/buidler/plugins");

global.Web3 = lazyFunction(() => require("web3"));
global.web3 = lazyObject(() => new global.Web3());

console.log(Web3.providers.HttpProvider.name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { lazyFunction, lazyObject } = require("@nomiclabs/buidler/plugins");

global.Web3 = lazyFunction(() => require("web3"));
global.web3 = lazyObject(() => new global.Web3());

console.log(global.web3.eth.getAccounts.name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { lazyFunction, lazyObject } = require("@nomiclabs/buidler/plugins");

global.Web3 = lazyFunction(() => require("web3"));
global.web3 = lazyObject(() => new global.Web3());

require("web3");
6 changes: 5 additions & 1 deletion packages/buidler-web3/scripts/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ else
start_ganache
fi

../../node_modules/.bin/mocha --exit
../../node_modules/.bin/mocha --exit

node web3-lazy-object-tests/when-accessing-web3-class.js
node web3-lazy-object-tests/when-accessing-web3-object.js
node web3-lazy-object-tests/when-requiring-web3-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { lazyFunction, lazyObject } = require("@nomiclabs/buidler/plugins");

global.Web3 = lazyFunction(() => require("web3"));
global.web3 = lazyObject(() => new global.Web3());

console.log(Web3.version);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { lazyFunction, lazyObject } = require("@nomiclabs/buidler/plugins");

global.Web3 = lazyFunction(() => require("web3"));
global.web3 = lazyObject(() => new global.Web3());

console.log(global.web3.eth.getAccounts.name);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { lazyFunction, lazyObject } = require("@nomiclabs/buidler/plugins");

global.Web3 = lazyFunction(() => require("web3"));
global.web3 = lazyObject(() => new global.Web3());

require("web3");

0 comments on commit 3e7301a

Please sign in to comment.